Class & Object
Class & Object
Class & Object
PUBLIC
class class_name
{
private: private members or methods
…
…
…
public:
… Public members or methods
…
…
};
This class example shows how we can
encapsulate (gather) a circle information into
one package (unit or class)
No need for others classes to access
class Circle and retrieve its value directly. The
{ class methods are responsible for
that only.
private:
double radius;
public:
void setRadius(double r); They are accessible from outside
double getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
Class Example (Problem)
#include<iostream.h> void main()
#include<stdio.h> {
class student student s;
{ cout<<“enter the rollno.:”;
int rollno; cin>>s.rollno;
char name[20]; cout<<“enter the name:”;
}; gets(s.name);
cout<<“rollno:”<<s.rollno;
cout<<“\nname:”;
puts(s.name);
}
Class Example (Solution)
#include<iostream.h> void main()
#include<stdio.h> {
class student student s;
{ cout<<“enter the rollno.:”;
public: cin>>s.rollno;
int rollno; cout<<“enter the name:”;
char name[20]; gets(s.name);
}; cout<<“rollno:”<<s.rollno;
cout<<“\nname:”;
puts(s.name);
}
Implementing class methods
There are two ways:
1. Member functions defined outside class
Using Binary scope resolution operator (::)
“Ties” member name to class name
Uniquely identify functions of particular class
Different classes can have member functions with same name
Format for defining member functions
ReturnType ClassName::MemberFunctionName( ){
…
}
Member Function
Defining Inside the Class
#include<iostream.h>
#include<stdio.h>
class student
{ Data Members (Private : in this example)
int rollno;
char name[20];
public:
void getdata()
{
cout<<“enter the rollno.:”; Member Functions (Public: in this example)
cin>>rollno;
cout<<“enter the name:”;
gets(name);
}
void putdata()
{
cout<<“rollno:”<<rollno;
cout<<“\nname:”;
puts(name);
}
};
Calling member function
void main()
{
student s;
s.getdata();
s.putdata();
}
Member Function
Defining Outside the Class
#include<iostream.h> void student: :: putdata()
#include<stdio.h> {
class student cout<<“rollno:”<<rollno;
{ cout<<“\nname:”;
int rollno; puts(name);
char name[20]; }
public: void main()
void getdata(); {
void putdata(); student s;
}; s.getdata();
void student :: getdata() s.putdata();
{ }
cout<<“enter the rollno.:”;
cin>>rollno;
cout<<“enter the name:”;
gets(name);
}
Characteristics of member
function
Different classes have same function name. the
“membership label” will resolve their scope.
Member functions can access the private data of the
class .a non member function cannot do this.(friend
function can do this.)
A member function can call another member function
directly, without using the dot operator.
Accessing Class Members
Operators to access class members
Identical to those for structs
Dot member selection operator (.)
Object
Reference to object
Arrow member selection operator (->)
Pointers
Static members
The data and functions of the class may be declared static in
the class declaration.
The static data members is initialized with zero when the first
object of its class is created. No other initialization is
permitted.
Only one copy of that member is created for the entire class
and is shared by all the objects of that class, no matter how
many objects are created.
It is visible only within the class, but its lifetime is the entire
program.
Static member function
Like static data members we can also declare static
member functions.
A static function can have access to only other static
members(functions or variables) declared in the
same class.
A static member function can be called using the
class (instead of its objects) as folows
Class name:: function name.
Example of static members Int test::count;
#inlcude<iostream.h> Int main()
Class test {
{
Int code;
test t1,t2;
Static int count; t1.setcode();
Public: t2.setcode();
Void setcode()
{ test::showcount();
Code=++count; test t3;
}
Void showcode() t3.setcode();
{ test::showcount();
Cout<<“object number “<<code<<endl; t1.showcode();
}
Static void showcount() t2.showcode();
{ t3.showcode();
Cout<<“count :”<<count;
} Return 0;
};
}
Class inside a function
When a class declared within a function, it is known as
local class.
A local class is known only to that function and
unknown outside it.
All member functions must be defined within the class
declaration.
The local class may not use local variables of the
function in which it is declared except static and extern
local variables declared within the function.
No static variables may be declared inside a local class.
Due to these restrictions local class is not popular in C++
programming.
Objects
An object is an instance of a class.
We have to declared objects close to the place where they are needed
because it makes easier to identify the objects.
Object types
There are four types of objects
1. External (global )objects
1. This object have the existence throughout the lifetime of the program and
having file –scope.
2. Automatic(local)objects
1. Persistent and visible only throughout the local scope in which they are
created.
3. Static objects
1. Persistent throughout a program but only visible within their local scope.
4. Dynamic objects
1. Lifetime may be controlled within a particular scope.
Memory Allocation of Object
class student
{ rollno – 2 bytes
int rollno;
char name[20]; name- 20 bytes
int marks;
};
student s; marks- 2 bytes
24 bytes s
Array of objects
2. Manager[i].put data();
dimensional array.
Object as function arguments
This can be done in two ways:-
(pass by reference)
When an address of object is passed, the called
function works directly on the actual object used in
the call. Means that any change made in side the
function will reflect in the actual object.
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( complex A, complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z;
cout<<“enter imaginary part:”; X.getdata( );
cin>>imag; Y.getdata( );
}
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( );
if (imag>=0)
}
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
}
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z;
cout<<“enter imaginary part:”; X.getdata( );
cin>>imag; Y.getdata( );
} X Y Z
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( );
if (imag>=0)
}
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
}
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
Complex X,Y,Z; 5 7
cin>>real;
6 8
cout<<“enter imaginary part:”; X.getdata( );
cin>>imag; Y.getdata( );
} X Y Z
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( );
if (imag>=0)
}
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
}
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
Complex X,Y,Z; 5 7
cin>>real;
6 8
cout<<“enter imaginary part:”; X.getdata( );
cin>>imag; Y.getdata( );
} X Y Z
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( );
if (imag>=0) 5 7
} 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”; A B
}
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = A.real + B.real;
public:
void getdata( ); imag= A.imag + B.imag;
void putdata( ); }
void sum(Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
Complex X,Y,Z; 5 7 12
cin>>real;
6 8 14
cout<<“enter imaginary part:”; X.getdata( );
cin>>imag; Y.getdata( );
} X Y Z
void Complex : : putdata( ) Z.sum(X,Y);
{ Z.putdata( ); + =
if (imag>=0) 5 7
} 6 8
cout<<real<<“+”<<imag<<“i”; + =
else
cout<<real<<imag<<“i”; A B
}
Passing Object
#include<iostream.h> void complex : : sum ( Complex A, Complex B)
class Complex {
{ real = A.real + B.real;
float real, imag; imag= A.imag + B.imag;
public: }
void getdata( );
void putdata( ); void main( )
void sum (Complex A, Complex B); {
}; Complex X,Y,Z;
void Complex : : getdata( ) X.getdata( );
{ Y.getdata( );
cout<<“enter real part:”; Z.sum(X,Y); 5 7 12
cin>>real;
cout<<“enter imaginary part:”;
Z.putdata( ); 6 8 14
}
cin>>imag;
} X Y Z
void Complex : : putdata( )
{ + =
if (imag>=0) 12 + 14 i 5 7
cout<<real<<“+”<<imag<<“i”; 6 + 8 =
else
cout<<real<<imag<<“i”; A B
}
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( )
{
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
}
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( )
{
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
}
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{ 6 8
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
}
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{ 6 8
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 7
8
B
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{ 6 8
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 7
8
B
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{ 6 8
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 7 12
8 14
B temp
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{ 6 8
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 7 12
8 14
B temp
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{ 6 8 14
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 7 12
8 14
B temp
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=real + B.real;
public: temp.imag= imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= X.sum (Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{ 6 8 14
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 7 12
12 + 14 i 8 14
B temp
C++ garbage collection
In c++ the garbage collection task is accomplised by
mark and sweep algorithm.
Once the memory allocated at the compile time then it can not
be expanded nor be compressed to accommodate more or less
data during program execution.
A B
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{ 6 8
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 5 + 7 =
6 + 8 =
A B temp
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{ 6 8
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 5 + 7 = 12
6 + 8 = 14
A B temp
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{ 6 8 14
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else X Y Z
cout<<real<<imag<<“i”;
} 5 + 7 = 12
6 + 8 = 14
A B temp
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; temp.real=A.real + B.real;
public: temp.imag= A.imag + B.imag;
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ X.Getdata( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; Z.putdata( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{ 6 8 14
if (imag>=0)
cout<<real<<“+”<<imag<<“i”; 12 + 14 i
else X Y Z
cout<<real<<imag<<“i”;
} 5 + 7 = 12
6 + 8 = 14
A B temp
We can also declare all the member functions of one
class as the friend functions of another class. In this
case the first class is known as FRIEND class.
We must add default values from right to left ,we can not
provide a default value to a particular arguments in the
middle of argument list.
Advantages:-
We can default arguments to add new parameters to the
existing functions.
Default arguments can be used to combine similar
functions into one.
Function overloading
When using more than one functions with same name and
with different arguments in a program is known as
function overloading or function polymorphism.
Examples;-
Int area(int,int);
Int area(int ,float);
Function overloading
Examples :-
Function prototype 1
Int add(int a, int b);
Int add(int a, int b, int c); Function prototype 2
Function calls
Add(5,19);
Add(16,7.9); Add(12.4,3.5);
Add (4,12,23); Add(3.4,7)
Function overloading
A function call first match the prototype having the same
number and type of actual arguments and then calls the
appropriate function for execution…
Function overloading
A function match includes following steps:-
4. If all of the steps fail then the compiler will try user
defined conversions in combination with integral
promotions and built in conversions to find a unique
match.
Constructors
and
Destructors
Constructor
It is a member function which initializes the
objects of its class.
A constructor has:
(i) the same name as the class itself
(ii) no return type ,not even void.
rc.posn(100, 100);
rc.draw();
rc.move(50, 50);
rc.draw();
}
and 67 to it.
Example:-
#include<iostream.h> Int main()
Class integer {
{ int m,n; Integer i1(10,100);
public: Integer i2=integer(33,55);
integer(int,int); Cout<<“object 1”;
void display() i1.display();
{ cout<<“m”<<m; Cout<<“object 2”;
cout<<“n”<<n; } i2.display();
}; Return 0;
Integer::integer(int x,int y) }
{
m=x;
n=y;
}
Notes:-
Expl:- ~assign()
{
Delete p;
}
#include<iostream.h> int main()
Int count =0; {
Class try cout<< “enter main”;
{ public: try t1,t2,t3,t4;
try() {
{ cout<<“block1”;
count++; try t5;
Cout<<“no of objects created”<<count; }
} {
~try() cout<<“block 2”;
{ try t6;
cout<<“no of object destroyed”<<count; }
Count- -; cout<<“again in main”;
}}; Return 0;
}