Class and Object
Class and 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); double They are accessible from outside
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>>[Link];
char name[20]; cout<<“enter the name:”;
}; gets([Link]);
cout<<“rollno:”<<[Link];
cout<<“\nname:”;
puts([Link]);
}
Class Example (Solution)
#include<iostream.h> void main()
#include<stdio.h> {
class student student s;
{ cout<<“enter the rollno.:”;
public: cin>>[Link];
int rollno; cout<<“enter the name:”;
char name[20]; gets([Link]);
}; cout<<“rollno:”<<[Link];
cout<<“\nname:”;
puts([Link]);
}
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
{
int rollno; example)
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;
[Link]();
[Link]();
}
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;
}; [Link]();
void student :: getdata() [Link]();
{ }
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; [Link]();
Public: [Link]();
Void setcode()
{ test::showcount();
Code=++count; test t3;
}
Void showcode() [Link]();
{ test::showcount();
Cout<<“object number “<<code<<endl;
}
[Link]();
Static void showcount() [Link]();
{ [Link]();
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
(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 = [Link] + [Link];
public:
void getdata( ); imag= [Link] + [Link];
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:”; [Link]( );
cin>>imag;
[Link]( );
}
void Complex : : putdata( ) [Link](X,Y);
{ [Link]( );
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 = [Link] + [Link];
public:
void getdata( ); imag= [Link] + [Link];
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:”; [Link]( );
cin>>imag;
[Link]( );
} X Y Z
void Complex : : putdata( ) [Link](X,Y);
{ [Link]( );
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 = [Link] + [Link];
public:
void getdata( ); imag= [Link] + [Link];
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z; 5 7
cout<<“enter imaginary part:”; [Link]( ); 6 8
cin>>imag;
[Link]( );
} X Y Z
void Complex : : putdata( ) [Link](X,Y);
{ [Link]( );
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 = [Link] + [Link];
public:
void getdata( ); imag= [Link] + [Link];
void putdata( ); }
void sum (Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z; 5 7
cout<<“enter imaginary part:”; [Link]( ); 6 8
cin>>imag;
[Link]( );
} X Y Z
void Complex : : putdata( ) [Link](X,Y);
{ [Link]( );
if (imag>=0) 5 7
cout<<real<<“+”<<imag<<“i”; }
else 6 8
cout<<real<<imag<<“i”;
} A B
Passing Object
#include<iostream.h>
class Complex void Complex : : sum ( Complex A, Complex B)
{ {
float real, imag; real = [Link] + [Link];
public:
void getdata( ); imag= [Link] + [Link];
void putdata( ); }
void sum(Complex A, Complex B);
};
void Complex : : getdata( ) void main( )
{ {
cout<<“enter real part:”;
cin>>real; Complex X,Y,Z; 5 7 12
cout<<“enter imaginary part:”; [Link]( ); 6 8 14
cin>>imag;
[Link]( );
} X Y Z
void Complex : : putdata( ) [Link](X,Y);
{ [Link]( );
if (imag>=0) 5 + 7 =
cout<<real<<“+”<<imag<<“i”; }
else 6 + 8 =
cout<<real<<imag<<“i”;
} A B
Passing Object
#include<iostream.h> void complex : : sum ( Complex A, Complex B)
class Complex {
{ real = [Link] + [Link];
float real, imag; imag= [Link] + [Link];
public: }
void getdata( );
void putdata( ); void main( )
void sum (Complex A, Complex B); {
}; Complex X,Y,Z;
void Complex : : getdata( ) [Link]( );
{ [Link]( );
cout<<“enter real part:”; [Link](X,Y);
cin>>real; 5 7 12
[Link]( );
cout<<“enter imaginary part:”; } 6 8 14
cin>>imag;
} X Y Z
void Complex : : putdata( )
{
if (imag>=0) 12 + 14 i 5 + 7 =
cout<<real<<“+”<<imag<<“i”;
else 6 + 8 =
cout<<real<<imag<<“i”;
} A B
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
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; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( )
{
if (imag>=0)
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7
8
B
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7
8
B
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7 12
8 14
tem
B
p
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7 12
8 14
tem
B
p
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
7 12
8 14
tem
B
p
Returning Object
#include<iostream.h> Complex Complex : : sum (Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=real + [Link];
public: [Link]= imag + [Link];
void getdata( ); return temp;
void putdata( ); }
Complex sum (Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= [Link] (Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
12 + 14 i 7 12
8 14
tem
B
p
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.
■ Int a[10];
Dynamic memory allocation
A B
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=[Link] + [Link];
public: [Link]= [Link] + [Link];
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 =
6 + 8 =
tem
A B
p
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=[Link] + [Link];
public: [Link]= [Link] + [Link];
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7
{
if (imag>=0) 6 8
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 = 12
6 + 8 = 14
tem
A B
p
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=[Link] + [Link];
public: [Link]= [Link] + [Link];
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”;
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 = 12
6 + 8 = 14
tem
A B
p
Friend Function
#include<iostream.h> Complex sum (Complex A, Complex B)
class Complex {
{ Complex temp;
float real, imag; [Link]=[Link] + [Link];
public: [Link]= [Link] + [Link];
void getdata( ); return temp;
void putdata( ); }
friend Complex sum (Complex A, Complex B); void main ( )
}; {
void Complex : : getdata( ) Complex X, Y, Z;
{ [Link]( );
cout<<“enter real part:”; Y. getdata( );
cin>>real; Z= sum (X,Y);
cout<<“enter imaginary part:”; [Link]( );
cin>>imag; }
}
void Complex : : putdata( ) 5 7 12
{
if (imag>=0) 6 8 14
cout<<real<<“+”<<imag<<“i”; 12 + 14 i
else
cout<<real<<imag<<“i”;
X Y Z
}
5 + 7 = 12
6 + 8 = 14
tem
A B
p
■ 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.
[Link](100, 100);
[Link]();
[Link](50, 50);
[Link]();
}
■ 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;
}