0% found this document useful (0 votes)
24 views14 pages

Member Function Same Name Void Return Type

A constructor is a member function with the same name as its class that is used to initialize variables when an object is created. A destructor is a member function with the same name as its class prefixed with a tilde that is used to clean up memory and resources when an object is destroyed. A friend function is a normal function declared as a friend of a class so it has access to the class's private and protected members, but is not a member of the class. A friend class is a class declared as a friend of another class so it has access to the other class's private members.

Uploaded by

Muhammad Sarmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views14 pages

Member Function Same Name Void Return Type

A constructor is a member function with the same name as its class that is used to initialize variables when an object is created. A destructor is a member function with the same name as its class prefixed with a tilde that is used to clean up memory and resources when an object is destroyed. A friend function is a normal function declared as a friend of a class so it has access to the class's private and protected members, but is not a member of the class. A friend class is a class declared as a friend of another class so it has access to the other class's private members.

Uploaded by

Muhammad Sarmad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Constructor:

• A constructor is a member function with the same


name as its class.
e.g. Part( arguments ){}
• Constructor do not return any value. Neither void nor
any return type.
• Constructors are used for initialization of the
variables.
• Constructors are called at a point an object is created.
• Default Constructor takes no arguments.
Destructor:
• A destructor is a member function with the same
name as its class prefixed by a ~ (tilde).
e.g. ~Part(){}
• Destructors are usually used to de-allocate memory
and do other cleanup for a class object and its class
members when the object is destroyed.
• A destructor takes no arguments and has no return
type.
Example 1: class smallobj
{
private:
int somedata;
In C++, the term public:
object refers to void setdata(int d)
an instance of a {
somedata = d ;
class.
}
void showdata( )
{
cout<<”Data is ” <<somedata<<endl;
}
};
void main ( )
{
smallobj s1,s2;
s1.setdata(100);
s2.setdata(150);
s1.showdata( );
s2.showdata( );
USING CLASS AND MEMBER FUNCTIONS

Q1. Write a program which works as a Money Exchanger.


• User Input: Dollars/Rupees
Rates of Money Exchanger are as follows:
The buying price of dollar is 60.04 Rs.
The selling price of dollar is 60.50 Rs.

Q2. Write a program using two classes, one for circle and the other for sphere.
Use member functions to calculate the area of both objects.
Friend Function:

• A friend function is a function that is not a member of a


class but has access to the class's private and protected
members.

• Friend functions are not considered class members; they


are normal external functions that are given special
access privileges.
Example 1: Friend Function
class car friend car display (car);
{ };
private: car display (car x)
int speed; {
int model; x.model++; 2011
public: x.speed=60;
void input( ) return x; 60
{ }
cout<<"Enter the model”; void main( )
cin>>model;
{
cout<<"Enter 2010
the speed”;
car mine;
cin>>speed;
100 mine.input( );
}
mine=display(mine);
void print( ) mine.print( );
{ getch();
cout<<speed; }
cout<<model;
}
Example 2: Friend Function
class Student void main()
{ {
int roll; Student s1;
string name; s1=input(s1);
public: output(s1);
Student() }
{ Student input(student s)
roll = 0; {
name = "Ali"; cin >> s.roll;
} cin >> s.name;
Student(int x, string s) return s;
{ }
roll = x; void output(Student s)
name = s; {
} cout << s.roll;
friend Student input(Student); cout << s.name;
friend void output(Student); }
};
Friend Function:

• A function can be declared as friend of more than one class.

• Friend Function must be declared in each class.


Example 3: Friend Function
class virus;
class bacteria class virus
{ {
private: private:
int life; int life;
public: public:
bacteria( ) virus( )
{ {
life=1; life = 0;
} }
friend void check (bacteria,virus); friend void check
}; (bacteria,virus);
};
Example 3 Continued: Friend Function
void check (bacteria b ,virus v) void main( )
{ {
if (b.life= =1 || v.life= =1) bacteria fever;
cout<<"\nSomething is alive"; virus cholera;
check (fever, cholera);
if (b.life = = 1) getch ( );
cout<<"\nA bacteria is alive."; }

if (v.life = = 1)
cout<<"\nA virus is alive.";
}
Example 4: Friend Function
class Tutorial int Add (int x)
{ {
int data; Tutorial var1;
friend int Add (int x); return var1.data + x;
}
public:
Tutorial ( )
{
void main( )
data = 5;
{
}
cout<<"Result"<<Add(4)<<endl;
};
getch();
}
REMEMBER :

• The friend functions are not member of any class, but


they can be used to access private data of one or
more classes to which they are friend. No other
function can do that!

• Since they are not members of any class, one should


not call them using the dot operator.

• One should declare the friend function in both the


classes where you want it to be a friend

• A friend declaration can be placed into either private


or public section of a class. it does not make any
difference. (Standard: public)
Friend Class:
• A class can be a friend of another class, which means, the
friend class will have access to all the private members of
the other class.
Example:
class Add void main( )
{ {
int x, y; Add ad;
public: Support sup;
Add( ) cout << “The sum of the 2
{ members
x=y=4; is: “ << sup.sum(ad);
getch ( );
}
}
friend class Support;
};
output:
class Support
{
public: The sum of the 2 members is: 8
int sum (Add ob)
{
return (ob.x + ob.y);
}
};

You might also like