0% found this document useful (0 votes)
13 views

8 Objects and Classes

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

8 Objects and Classes

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

CLASSES AND

Objects and
OBJECTS Classes
Class
• A class is a way to bind data and associated function
together.
• A class is an expanded concept of a data structure,
instead of
holding only data , it can hold both data and function.
• The data is to be hidden from external use.
• Classes are generally declared using the
keyword class, with the following format:
class class_name
{
private:
variable declaration;
public:
function declaration;

• The body of the declaration can contain members
that can be either data or function declaration,
and optionally access specifier.

• The variable declared inside the class is known as


data member and function are known as
member functions.

• Access specifier are keyword in object oriented


language that set the accessibility of classes,
method and other member.

• Access specifier is one of the following keyword:


public, private, protected.
• These specifier modify the access rights that the member
following
them acquire:
 private members of class are accessible only from
within other member of same class or from their friends.
 protected members are accessible from members
of their same class and from their friends but also from
members of their derived classes.
 public members are accessible from anywhere the
object is visible.

• By default, all members of class declared with the class


keyword have private access for all its member. Therefore,
any member that is declared before one other class
specifier automatically has private access.

• private functionfunction
Only member of that can have access to private data
Object
• Once a class has been created, we can create
variable of that type(class type) by using following
syntax which is called object.
• Syntax:
class_name variable_name;
Ex:
student s;
• we can create any number of objects belonging
to that class by declaring more than one object in
one statement. This statement are written in
main().

• The objects can also be defined by placing their


name immediately after the closing brace of the
class.
• Syntax:
class
class_name
{
….
}object1,object2
,…;

• Ex:

class student
{

}s1,s2;
• Accessing class member:

A object can be declared in the main(),and


member functions are declared in class in public
section so always a member function can be
called by using object.
• Syntax:
object_name.member_function(arguments);
Ex:
s.getdata();

• A data member can also be access by using


object only , if data member is declared as
public.

• If data member is declared private then you can


Defining member
function
• A member function can be defined in two places in
the class:
1. inside the class definition
2. outside the class definition

1) Inside the class definition:


To write a member function inside the class
instead of only declaration(prototype).

Ex:
class item
{
int
num;
public:
void getdata(int
a,float b) void
putdata(void)
{
cout<<numb
er;
cout<<cost;
}
};
….
2) Outside the class definition:
• To write function we need to declare function
inside the class and definition(function body) is
written outside the class.
• The general form of a member function
definition:

return_type
class_name::function_name(arument)
{
function body
}
• The membership label class_name :: tells
the compiler that the function
function_Name belongs to the class
Ex:
….
void item::getdata(int a,
float b)
{
number=
a;
coat=b;
}
void
item::putdat
a(void)
{
cout<<“number
“<<number; cout<<
“cost ”<<cost;
Scope Resolution Operator(::)
• It is used to define a function outside a class.
#include<iostream>
using namespace std;

class A
{
public:

// Only declaration
void fun();
};
// Definition outside class using ::
void A::fun()
{
cout << "fun() called";
}

int main()
{
A a;
a.fun();
return 0;
}
Object passing as an argument:

To pass an object as an argument we write the


object name as the argument
while calling the function the same way we do it for
other variables.
Syntax:
function_name(object_name);
C++ program to show passing of objects to a
function
#include <bits/stdc++.h>
using namespace std;

class Example {
public:
int a;

// This function will take


// an object as an argument
void add(Example E)
{
a = a + E.a;
}
};
Contd..

// Driver Code
int main()
{

// Create objects
Example E1, E2;

// Values are initialized for both


objects
E1.a = 50;
E2.a = 100;

cout << "Initial Values \n";


cout << "Value of object 1: " << E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";
Contd..
// Passing object as an argument
// to function add()
E2.add(E1);

// Changed values after passing


// object as argument
cout << "New values \n";
cout << "Value of object 1: " <<
E1.a
<< "\n& object 2: " << E2.a
<< "\n\n";

return 0;
}
Output:
Initial Values Value of object 1:
50
& object 2: 100
New values Value of object 1: 50
& object 2: 150
Returning Object as argument

• Syntax:
object = return object_name;
#include <bits/stdc++.h>
using namespace std;

class Example {
public:
int a;

// This function will take


// object as arguments and
// return object
Example add(Example Ea, Example Eb)
{
Example Ec;
Ec.a = Ea.a + Eb.a;

// returning the object


return Ec;
}
};
int main()
{
Example E1, E2, E3;

// Values are initialized


// for both objects
E1.a = 50;
E2.a = 100;
E3.a = 0;

cout << "Initial Values \n";


cout << "Value of object 1: " <<
E1.a
<< ", \nobject 2: " << E2.a
<< ", \nobject 3: " << E3.a
<< "\n";

// Passing object as an argument


// to function add()
E3 = E3.add(E1, E2);
// Changed values after
// passing object as an argument
cout << "New values \n";
cout << "Value of object 1: " <<
E1.a
<< ", \nobject 2: " << E2.a
<< ", \nobject 3: " << E3.a
<< "\n";

return 0;
}
Output

Initial Values Value of object 1:


50,
object 2: 100,
object 3: 0
New values Value of object 1: 50,
object 2: 100,
object 3: 150
C++ Assigning Objects

• If both objects are of the same type (that is, both are objects of the
same class), then one object may be assigned to another.
#include <iostream>
using namespace std;

class myclass {
int a, b;
public:
void setab(int i, int j) { a = i, b = j; }
void showab();
};
void myclass::showab()
{
cout << "a is " << a << '\n';
cout << "b is " << b << '\n';
}
int main()
{
myclass ob1, ob2;
ob1.setab(10, 20);
ob2.setab(0, 0);
cout << "ob1 before assignment: \n";
ob1.showab();
cout << "ob2 before assignment: \n";
ob2.showab();
cout << '\n';
ob2 = ob1; // assign ob1 to ob2
cout << "ob1 after assignment: \n";
ob1.showab();
cout << "ob2 after assignment: \n";
ob2.showab();
return 0;
}

You might also like