Professional Documents
Culture Documents
&
OBJECTS
1
OBJECT
An object is a collection of some properties, behavior with
some existence.
Box object:
Properties:- length , breadth, width. ( data elements)
Behavior:- open, close. ( functions )
Existence:- length=15, breadth=10, width=5 ( data values )
length
length 15
Data values
Data
elements breadth
breadth 10
width
width 5
open()
open()
A Box object
close()
close()
Functions 2
CLASS
Class is a collection of some properties, behavior
BOX class:
Properties:- length , breadth, width. ( data elements)
Behavior:- open, close. ( functions )
length
length Data
breadth
breadth elements
width
width
open()
open()
Functions
close()
close() 3
Box class
CLASS AND OBJECTS
4
PARTS IN A CLASS
class
Class
Class is a keyword in C++ which is used to create user-defined
data-types.They are also known as Abstract data type(ADT) and this 5
NOTE !!! 6
7
AN EXAMPLE(WAY
1)
Class Name
class A{
private: Member Data
int num;
public: Access Specifiers
void get()
Member Functions
cin>>num;
void display()
cout<<num;
};
8
WAY 2
class A{
int num;
public:
void get()
cin>>num;
void display()
cout<<num;
};
Syntax:
<Class_Name> <Object_Name>;
Example:
A obj;
10
ACCESSING MEMBER FUNCTIONS
The member functions of a class can be accessed by
using dot(.) operator.
Syntax:
<Object_Name>.<Member_Function_Name>;
or
<Object_Name>.<Member_Variable_Name>;
Example:
obj.fun();
11
DEFINING A MEMBER FUNCTIONS
The member can defined in two ways:
Within the class definition
Outside the class definition
12
DEFINE A MEMBER FUNCTION (NON-STATIC)
class Time{
private:
int hour;
int min;
int sec;
public:
void setTime(int h,int m,int s){
hour=h;
min=m; class name
sec=s;
}
member function name
void printTime(void);
};
Scope-Resolution operator
STRUCTURE VS. CLASS IN C++
• A structure is simply a class whose members are public by
default.
Example: struct Time{
class Time{
int hour;
int hour;
Private by int min; Public by
int min;
int sec; default int sec; default
public:
public:
void setTime(int,int,int);
void setTime(int,int,int);
void printTime(void)
void printTime(void);
};
};
14
DECLARATION OF AN OBJECT
Similar to declaration of a structure variable in C++.
struct emp e1; emp e1; class Time t1; Time t1;
struct emp e2,e3,e4; emp e2,e3,e4; class Time t2,t3,t4; Time t2,t3,t4;
class Time{
private:
main(){
int hour,min,sec;
public: Time t;
void setTime(int h,int m,int s){ t.setTime(13,27,6);
hour=h; min=m; sec=s;
t.printTime();
}
}
void printTime(void);
}; t hour = 13
void Time::printTime(){ min = 27
cout <<hour << “:” << min << “:” << sec = 6 15
sec << endl;
} 13:27:6
UNDERSTANDING PRIVATE AND PUBLIC
class X{ class X{
class X{
public: int a; int a;
int a; }; public:
}; void set(int b){
a=b;
}
main(){ main(){
void get(){
X x1; X x1;
x1.a=5; x1.a=5; //Error cout<<a;
cout<<a; //Error cout<<a; //Error }
cout<<x1.a; // 5 cout<<x1.a; //Error };
} }
main(){
X x1;
x1.set(5);
x1.get(); // 5 16
}
MEMBER FUNCTION IN PRIVATE
class X{ main(){
int a;
X x1;
void disp(int b){
a=b; x1.disp(5);
// Error
cout<<a; }
}
};
17
PROPERTIES OF MEMBER FUNCTION()
Several classes can have member function with same name.
Member function can access all the data members inside the class
irrespective of their access specifiers.
One member function can call another member function of same class
directly without using dot operator. Such a mechanism is called nested
member function.
Member functions defined inside the class are inline by default, but the
outside defined member functions are to be made inline explicitly if needed
class X{
int a;
main(){
void disp(int b){
a=b; X x1;
cout<<a; x1.disp(5); // Error
} x1.call_disp(5);
public:
void call_disp(int c){ }
disp(c);
18
}
};
MEMORY ALLOCATION FOR OBJECTS
Common for all objects
Member fun 1
Member fun 2
Memory created when
functions defined.
19
Memory created when
objects created.
THINGS TO REMEMBER …
When class is defined memory is allocated for member
functions but not for member variables.
When we declare an object then member variables get
memory allocated.
Member functions get memory only once.
20
MEMBER FUNCTIONS WITHOUT TAKING
ARGUMENTS
class A void main()
{ {
int a , b; A obj;
public: obj . getData();
void getData() obj.sum();
{ }
cout<<“Enter the values of
a and b”;
cin>>a>>b;
}
void sum()
cout<<a+b;
21
};
MEMBER FUNCTIONS TAKING
ARGUMENTS
class A void main()
{ {
int x , y; A obj;
public: obj . getData(7,8);
void getData(int a , int b ) obj.sum();
{ }
x = a;
y = b;
}
void sum()
cout<<x+y;
};
22
MEMBER FUNCTIONS TAKING OBJECTS
AS ARGUMENTS
class A void main()
{ {
int x , y; A ob1,ob2;
public: ob1 . getData(7,8);
void getObjects(A obj1 , A obj2 ) ob2 . getData(4,7);
{ A ob3;
x = obj1.x; ob3.getObjects(ob2,ob1);
y = obj2.y; ob3.getObjects(ob1,ob2);
cout<<x+y<<endl ; } }
void getData(int a , int b )
{
x = a;
y = b;
23
cout<<x+y<<endl; }
};
STATIC MEMBER VARIABLE
24
class X{
main(){
int a;
public: X x1,x2,x3;
static int b; x1.incr(); // 6
void incr(){ x2.incr(); // 7
a=10;
x3.incr(); // 8
b++;
cout<<b; cout<<X::b; // 8
} }
};
int X::b=5; // default ininitalization
Static variables are called
// value is zero as class variables.
Initialization syntax
<data-type> <class name> :: <var-name> = <value>;
25
STATIC MEMBER FUNCTION
It gets its memory allocated at the time of class definition as like a non-
static member function.
It can access other static members only (static member variable and other
static member function)
Like static member variable it can be accessed by class name and ::
operator, hence are called as class function.
ERROR: ‘b’ is
private member.
class X{ main(){
public:
X p[3];
int a,b;
int i;
};
p[0].a=10;
for(i=0;i<3;i++){
p[0].b=20;
cin>>p[i].a>>p[i].b;
cout<<p[0].a<<p[0].b;
}
110 a for(i=0;i<3;i++){
P[0]
112 b p[1].a=15;
cout<<p[i].a<<p[i].b;
114 a p[1].b=25;
}
P[1]
116 b } cout<<p[1].a<<p[1].b;
118 a
b P[2] p[2].a=50;
120
p[2].b=60;
cout<<p[2].a<<p[2].b;
32
}
CONST MEMBER FUNCTION
class Time
{
private :
function declaration
int hrs, mins, secs ;
public :
}; function definition