SlideShare a Scribd company logo
1 of 11
Download to read offline
1 | P a g e A n a n d a K u m a r H N
UNIT - 03 / CLASSES AND OBJECT - II
Friend Function:
class x{
public:
float a,b;
void add()
{
cout<<"add:"<<a+b<<endl;
}
friend void avg(x); //friend function declaration
using friend prefix
};
void avg(x x1) //function definition
{
cout<<"avg:"<<((x1.a+x1.b)/2);
}
int main()
{
x a,b;
cout<<"enter two valuesn";
cin>>a.a>>a.b;
a.add(); //member function call using object
reference
avg(a); // no need of object reference since it is
friend function
}
OUTPUT:
Friend Classes:
class x
{
private: int a,b; //private data members
public:
//initializing data members of class x
void init(int u, int t)
{
a=u;
b=t;
}
friend class y; //making class y as friend of x
};
class y
{
public:
int m,n;
//initializing data members of class y
void init(int q, int p)
{
m=q;
n=p;
}
// member function of class of y is accessing private
data members of class of x
void print(x x1)
{
cout<<"adding a+m:"<<x1.a+m<<endl;
cout<<"adding b+n:"<<x1.b+n<<endl;
}
};
int main()
{
x xx;
xx.init(11,22);
y yy;
yy.init(33,44);
yy.print(xx);
}
OUTPUT:
Friend Classes with common non member
function as friend:
class y; //forward declaration of class y
class x{
int a,b; //private data members
2 | P a g e A n a n d a K u m a r H N
public:
int e,f; //public data members
//initializing data members of class x
void initx(int x,int y,int o,int n)
{
a=x;
b=y;
e=o;
f=n;
}
//making non member function as friend of class x
friend void show(x x1,y y1);
friend class y; //making class y as friend of class x
};
class y{
int c,d;
public:
//initializing data members of class y
void inity(int t, int u)
{
c=t;
d=u;
}
//making non member function as friend of class x
friend void show(x x1,y y1);
};
//definition of non member of function
void show(x x1,y y1)
{
cout<<"a:"<<x1.a<<endl;
cout<<"b:"<<x1.b<<endl;
cout<<"c:"<<y1.c<<endl;
cout<<"d:"<<y1.d<<endl;
cout<<"a+c="<<x1.a+y1.c<<endl;
cout<<"b+d="<<x1.b+y1.d<<endl;
}
int main()
{
x x1;
y y1;
x1.initx(10,20,3,4);
y1.inity(25,40);
show(x1,y1); //calling friend function without object
reference
}
OUTPUT:
Passing objects as arguments:
class x{
public:
int a,b;
void add(x x1) //reciving object as a parameter
{
cout<<"add:"<<x1.a+x1.b<<endl;
}
float avg(x); //member function declaration
};
float x::avg(x x2) //defining function outside the class
{
return ((x2.a+x2.b)/2.0); //returning float value
}
int main()
{
x n1,n2;
cout<<"enter two valuesn";
cin>>n1.a>>n1.b;
n2.add(n1); //passing object n1 as parameter
cout<<"avg:"<<n2.avg(n1);
}
OUTPUT:
Returning objects:
class complex{
private:
int real;
int img;
2 | P a g e A n a n d a K u m a r H N
public:
int e,f; //public data members
//initializing data members of class x
void initx(int x,int y,int o,int n)
{
a=x;
b=y;
e=o;
f=n;
}
//making non member function as friend of class x
friend void show(x x1,y y1);
friend class y; //making class y as friend of class x
};
class y{
int c,d;
public:
//initializing data members of class y
void inity(int t, int u)
{
c=t;
d=u;
}
//making non member function as friend of class x
friend void show(x x1,y y1);
};
//definition of non member of function
void show(x x1,y y1)
{
cout<<"a:"<<x1.a<<endl;
cout<<"b:"<<x1.b<<endl;
cout<<"c:"<<y1.c<<endl;
cout<<"d:"<<y1.d<<endl;
cout<<"a+c="<<x1.a+y1.c<<endl;
cout<<"b+d="<<x1.b+y1.d<<endl;
}
int main()
{
x x1;
y y1;
x1.initx(10,20,3,4);
y1.inity(25,40);
show(x1,y1); //calling friend function without object
reference
}
OUTPUT:
Passing objects as arguments:
class x{
public:
int a,b;
void add(x x1) //reciving object as a parameter
{
cout<<"add:"<<x1.a+x1.b<<endl;
}
float avg(x); //member function declaration
};
float x::avg(x x2) //defining function outside the class
{
return ((x2.a+x2.b)/2.0); //returning float value
}
int main()
{
x n1,n2;
cout<<"enter two valuesn";
cin>>n1.a>>n1.b;
n2.add(n1); //passing object n1 as parameter
cout<<"avg:"<<n2.avg(n1);
}
OUTPUT:
Returning objects:
class complex{
private:
int real;
int img;
2 | P a g e A n a n d a K u m a r H N
public:
int e,f; //public data members
//initializing data members of class x
void initx(int x,int y,int o,int n)
{
a=x;
b=y;
e=o;
f=n;
}
//making non member function as friend of class x
friend void show(x x1,y y1);
friend class y; //making class y as friend of class x
};
class y{
int c,d;
public:
//initializing data members of class y
void inity(int t, int u)
{
c=t;
d=u;
}
//making non member function as friend of class x
friend void show(x x1,y y1);
};
//definition of non member of function
void show(x x1,y y1)
{
cout<<"a:"<<x1.a<<endl;
cout<<"b:"<<x1.b<<endl;
cout<<"c:"<<y1.c<<endl;
cout<<"d:"<<y1.d<<endl;
cout<<"a+c="<<x1.a+y1.c<<endl;
cout<<"b+d="<<x1.b+y1.d<<endl;
}
int main()
{
x x1;
y y1;
x1.initx(10,20,3,4);
y1.inity(25,40);
show(x1,y1); //calling friend function without object
reference
}
OUTPUT:
Passing objects as arguments:
class x{
public:
int a,b;
void add(x x1) //reciving object as a parameter
{
cout<<"add:"<<x1.a+x1.b<<endl;
}
float avg(x); //member function declaration
};
float x::avg(x x2) //defining function outside the class
{
return ((x2.a+x2.b)/2.0); //returning float value
}
int main()
{
x n1,n2;
cout<<"enter two valuesn";
cin>>n1.a>>n1.b;
n2.add(n1); //passing object n1 as parameter
cout<<"avg:"<<n2.avg(n1);
}
OUTPUT:
Returning objects:
class complex{
private:
int real;
int img;
5 | P a g e A n a n d a K u m a r H N
class emp
{
private:
char name[50];
int ID;
float salary;
public:
void read()
{
cout<<"enter name, ID and salaryn";
cin>>name>>ID>>salary;
}
void display( )
{
cout<<"NAME"<<"t"<<"ID"<<"t"<<"SALARYn";
cout<<name<<"t"<<ID<<"t"<<salary<<endl;
}
};
int main()
{
emp *e;
e=new emp;
e->read();
e->display();
}
OUTPUT:
class emp
{
private:
char name[50];
int ID;
float salary;
public:
void read( )
{
cout<<"enter name, ID and salaryn";
cin>>name>>ID>>salary;
}
void display( )
{
cout<<name<<"t"<<ID<<"t"<<salary<<endl;
}
};
int main()
{
int sizee;
cout<<"enter how many employee:"<<endl;
cin>>sizee;
emp *e=new emp[sizee];
for(int i=1;i<=sizee;i++)
{
cout<<"enter "<<i<<" employe name ID
salary:"<<endl;
e[i].read();
}
cout<<"employe details:"<<endl<<endl;
cout<<"NAME"<<"t"<<"ID"<<"t"<<"SALARY"<<e
ndl;
for(int i=1;i<=sizee;i++)
{
e[i].display();
}
delete [ ] e;
}
6 | P a g e A n a n d a K u m a r H N
class student{
public:
char name[30];
int usn;
void read()
{
cin>>name>>usn;
}
void show()
{
cout<<name<<"t"<<usn<<endl;
}
};
int main()
{
student *s;
s=new student[3];
for(int i=1;i<=3;i++)
{
cout<<"enter name and usn for student "<<i;
s[i].read();
}
cout<<"nametusnn";
for(int i=1;i<=3;i++)
{
s[i].show( );
}
delete [ ] s;
}
OUTPUT:
Pointers to objects:
class emp
{
private:
char name[50];
int ID;
float salary;
public:
void read()
{
cout<<"enter name, ID and salaryn";
cin>>name>>ID>>salary;
}
void display()
{
cout<<"NAME"<<"t"<<"ID"<<"t"<<"SALARYn";
cout<<name<<"t"<<ID<<"t"<<salary<<endl;
}
};
int main()
{
emp e;
7 | P a g e A n a n d a K u m a r H N
emp *d;
d=&e; // pointer to an object;
d->read(); //arrow to operator to access fuction
d->display();
}
class a
{
public:
int num;
a()
{
num=0;
}
a(int x)
{
num=x;
}
int get()
{
return num;
}
};
int main()
{
a a1(100);
a a2[5]={1,2,3,4,5};
a *x,*y;
x=&a1;
y=&a2[0];
cout<<x->get()<<endl;
for(int i=0;i<5;i++)
{
cout<<y->get()<<endl;
y++;
}
}
Copy constructors:
class X
{
public:
int a, b;
X()
{
cout<<"default constructor"<<endl;
}
void init(int x,int y)
{
a=x;
b=y;
}
X(X &m)
{
a=m.a;
b=m.b;
cout<<"copy constructor calledn";
}
void show()
{
cout<<"a:"<<a<<endl<<"b:"<<b<<endl;
}
};
int main()
{
X x1,x2; //default constructor is called
x1.init(99,88);
x1.show();
x2.init(55,11);//copy constructor is called
x2.show();
X x3=x1;//copy constructor is called
X x4(x2);//copy constructor is called
x3.show();
x4.show();
X x5;
x5=x3;// just copy , copy constructor is not called
x5.show();
}
8 | P a g e A n a n d a K u m a r H N
/*All Type of CONSTRUCTORS*/
class X
{
public:
int a, b;
X( )
{
a=b=0;
cout<<"default constructor"<<endl;
}
X(int x)
{ a=b=x;
cout<<"constructor with one parametern";
}
X(int x,int y)
{
a=x;
b=y;
cout<<"constructor with two parameter"<<endl;
}
X(X &m)
{
a=m.a;
b=m.b;
cout<<"copy constructor calledn";
}
void show()
{
cout<<"a:"<<a<<endl<<"b:"<<b<<endl;
}
};
int main()
{
X x1; //default constructor is called
x1.show();
X x2(100);//constructor with single parameter
x2.show();
X x3(29,99);//constructor with two parameter
x3.show();
X x4=x3;//copy constructor is called
x4.show();
X x5(x2);//copy constructor is called
x5.show();
x5=x3;// just copy , copy constructor is not called
x5.show();
}
Generic functions:
/*Generic function with ONE Type*/
template <class T1>
void myfunc(T1 X)
{
cout<<X<<endl;
}
int main()
{
myfunc(10);
myfunc("ATME-570028");
myfunc(3333.333);
myfunc("3rd SEMESTER");
myfunc(0.7685);
}
9 | P a g e A n a n d a K u m a r H N
/*Generic function with TWO Type*/
template <class T1,class T2>
void myfunc(T1 X,T2 Y)
{
cout<<X<<"tt"<<Y<<endl;
}
int main()
{
myfunc(10,25);
myfunc(11.222,44.567);
myfunc("ATME","570028");
myfunc(55,76.666);
myfunc("atme mysore",570028);
}
/*Overloading Generic Fuctions*/
template <class T1>
void myfunc(T1 X)
{
cout<<"single parameter: "<<X<<endl;
}
template <class T1,class T2>
void myfunc(T1 X,T2 Y)
{
cout<<"TWO parameters: "<<X<<" "<<Y<<endl;
}
int main()
{
myfunc(10,25.555);
myfunc(55,"ATME-570028");
myfunc(3333.333);
myfunc("atme","mysore");
myfunc(100,200);
myfunc("3rd Semester");
}
/*Overriding Generic Fuction*/
template <class T1>
void myfunc(T1 X)
{
cout<<"single parameter: "<<X<<endl;
}
template <class T1,class T2>
void myfunc(T1 X,T2 Y)
{
cout<<"TWO parameter: "<<X<<" "<<Y<<endl;
}
void myfunc(int m,int n)
{
cout<<"inside fuction myfuc: "<<m<<" "<<n<<endl;
}
int main()
{
myfunc(10,25.555); //calls generic function
myfunc(55,"ATME-570028"); //calls
generic function
myfunc(3333.333); //calls generic function
myfunc(1,2); //overrides generic function
myfunc("atme mysore"); //calls generic function
myfunc(100,200); //overrides generic function
}
Generic CLASSES:
/*Generic CLASS with ONE type*/
template <class T>
class x
{
public: T a;
T b;
10 | P a g e A n a n d a K u m a r H N
x(T m,T n)
{
a=m;
b=n;
}
void add()
{
cout<<”sum(a,b)=”<<a+b<<endl;
}
};
int main()
{
x<int>x1(111,222);
x<float>x2(2.33,4.7);
x<double>x3(22.33,44.77);
x1.add();
x2.add();
x3.add();
}
/*Generic CLASS with TWO types*/
template <class T1,class T2>
class x
{
public: T1 a;
T2 b;
x(T1 m,T2 n)
{
a=m;
b=n;
}
void add()
{
cout<<"sum(a,b)="<<a+b<<endl;
}
};
int main()
{
x<int,int>x1(11,222);
x<int,float>x2(2,4.7);
x<double,float>x3(33.333,55.555);
x1.add();
x2.add();
x3.add(); }
Operator overloading
/*overloaing unary + operator
as a member fuction*/
class a
{
public:
int m,n;
void init(int x, int y)
{
m=x;
n=y;
}
//overloaing as member fuction;
a operator+()
{
a t;
t.m=+m;
t.n=+n;
return t;
}
void show()
{
cout<<m<<endl<<n<<endl<<endl;
}
};
int main()
{
a aa,cc;
aa.init(-1,-1);
aa.show();
cc=+aa;
cc.show();
}
/*overloaing unary - operator
as a member fuction*/
class a
{
public:
int m,n;
void init(int x, int y)
{
m=x;
n=y;
}
11 | P a g e A n a n d a K u m a r H N
a operator-( )
{
a t;
t.m=-m;
t.n=-n;
return t;
}
void show( )
{
cout<<m<<endl<<n<<endl<<endl;
}
};
int main( )
{
a aa,bb,cc;
aa.init(-1,-1);
bb.init(-20,-40);
aa.show( );
bb.show( );
cc=-aa;
cc.show( );
cc=-bb;
cc.show( );
}

More Related Content

What's hot

CQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationCQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationSamuel ROZE
 
Patterns for slick database applications
Patterns for slick database applicationsPatterns for slick database applications
Patterns for slick database applicationsSkills Matter
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyondFrancis Johny
 
Using Scala Slick at FortyTwo
Using Scala Slick at FortyTwoUsing Scala Slick at FortyTwo
Using Scala Slick at FortyTwoEishay Smith
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashBret Little
 
Indexing thousands of writes per second with redis
Indexing thousands of writes per second with redisIndexing thousands of writes per second with redis
Indexing thousands of writes per second with redispauldix
 
A evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no androidA evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no androidRodrigo de Souza Castro
 
The Ring programming language version 1.6 book - Part 35 of 189
The Ring programming language version 1.6 book - Part 35 of 189The Ring programming language version 1.6 book - Part 35 of 189
The Ring programming language version 1.6 book - Part 35 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88Mahmoud Samir Fayed
 
Pybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in PythonPybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in PythonChristoph Matthies
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax BasicsRichard Paul
 
Collection v3
Collection v3Collection v3
Collection v3Sunil OS
 

What's hot (20)

Javascript
JavascriptJavascript
Javascript
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Python speleology
Python speleologyPython speleology
Python speleology
 
CQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony applicationCQRS and Event Sourcing in a Symfony application
CQRS and Event Sourcing in a Symfony application
 
Patterns for slick database applications
Patterns for slick database applicationsPatterns for slick database applications
Patterns for slick database applications
 
ECMAScript 6 and beyond
ECMAScript 6 and beyondECMAScript 6 and beyond
ECMAScript 6 and beyond
 
Oop lecture9 13
Oop lecture9 13Oop lecture9 13
Oop lecture9 13
 
Xm lparsers
Xm lparsersXm lparsers
Xm lparsers
 
Mattbrenner
MattbrennerMattbrenner
Mattbrenner
 
Using Scala Slick at FortyTwo
Using Scala Slick at FortyTwoUsing Scala Slick at FortyTwo
Using Scala Slick at FortyTwo
 
JavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and LodashJavaScript Fundamentals with Angular and Lodash
JavaScript Fundamentals with Angular and Lodash
 
Indexing thousands of writes per second with redis
Indexing thousands of writes per second with redisIndexing thousands of writes per second with redis
Indexing thousands of writes per second with redis
 
Academy PRO: ES2015
Academy PRO: ES2015Academy PRO: ES2015
Academy PRO: ES2015
 
A evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no androidA evolução da persistência de dados (com sqlite) no android
A evolução da persistência de dados (com sqlite) no android
 
The Ring programming language version 1.6 book - Part 35 of 189
The Ring programming language version 1.6 book - Part 35 of 189The Ring programming language version 1.6 book - Part 35 of 189
The Ring programming language version 1.6 book - Part 35 of 189
 
The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88The Ring programming language version 1.3 book - Part 83 of 88
The Ring programming language version 1.3 book - Part 83 of 88
 
Pybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in PythonPybelsberg — Constraint-based Programming in Python
Pybelsberg — Constraint-based Programming in Python
 
Javascript & Ajax Basics
Javascript & Ajax BasicsJavascript & Ajax Basics
Javascript & Ajax Basics
 
Collection v3
Collection v3Collection v3
Collection v3
 

Similar to C++ prgms 3rd unit (20)

New presentation oop
New presentation oopNew presentation oop
New presentation oop
 
Opp compile
Opp compileOpp compile
Opp compile
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
C++ programs
C++ programsC++ programs
C++ programs
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 
Inheritance in C++.ppt
Inheritance in C++.pptInheritance in C++.ppt
Inheritance in C++.ppt
 
Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exception
 
Pre zen ta sion
Pre zen ta sionPre zen ta sion
Pre zen ta sion
 
oop objects_classes
oop objects_classesoop objects_classes
oop objects_classes
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
class and objects
class and objectsclass and objects
class and objects
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Constructor in c++
Constructor in c++Constructor in c++
Constructor in c++
 
Constructor
ConstructorConstructor
Constructor
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
OOPS 22-23 (1).pptx
OOPS 22-23 (1).pptxOOPS 22-23 (1).pptx
OOPS 22-23 (1).pptx
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture4
Lecture4Lecture4
Lecture4
 
Cquestions
Cquestions Cquestions
Cquestions
 

More from Ananda Kumar HN

VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programsAnanda Kumar HN
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questionsAnanda Kumar HN
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introductionAnanda Kumar HN
 
C++ prgms io file unit 7
C++ prgms io file unit 7C++ prgms io file unit 7
C++ prgms io file unit 7Ananda Kumar HN
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineersAnanda Kumar HN
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADAAnanda Kumar HN
 

More from Ananda Kumar HN (9)

DAA 18CS42 VTU CSE
DAA 18CS42 VTU CSEDAA 18CS42 VTU CSE
DAA 18CS42 VTU CSE
 
CGV 18CS62 VTU CSE
CGV 18CS62 VTU CSECGV 18CS62 VTU CSE
CGV 18CS62 VTU CSE
 
Python for beginners
Python for beginnersPython for beginners
Python for beginners
 
VTU Network lab programs
VTU Network lab   programsVTU Network lab   programs
VTU Network lab programs
 
VTU CN-1important questions
VTU CN-1important questionsVTU CN-1important questions
VTU CN-1important questions
 
Oop with c++ notes unit 01 introduction
Oop with c++ notes   unit 01 introductionOop with c++ notes   unit 01 introduction
Oop with c++ notes unit 01 introduction
 
C++ prgms io file unit 7
C++ prgms io file unit 7C++ prgms io file unit 7
C++ prgms io file unit 7
 
Microsoft office in kannada for begineers
Microsoft office in kannada for begineersMicrosoft office in kannada for begineers
Microsoft office in kannada for begineers
 
Microsoft office in KANNADA
Microsoft office in KANNADAMicrosoft office in KANNADA
Microsoft office in KANNADA
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........LeaCamillePacle
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 

Recently uploaded (20)

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........Atmosphere science 7 quarter 4 .........
Atmosphere science 7 quarter 4 .........
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 

C++ prgms 3rd unit

  • 1. 1 | P a g e A n a n d a K u m a r H N UNIT - 03 / CLASSES AND OBJECT - II Friend Function: class x{ public: float a,b; void add() { cout<<"add:"<<a+b<<endl; } friend void avg(x); //friend function declaration using friend prefix }; void avg(x x1) //function definition { cout<<"avg:"<<((x1.a+x1.b)/2); } int main() { x a,b; cout<<"enter two valuesn"; cin>>a.a>>a.b; a.add(); //member function call using object reference avg(a); // no need of object reference since it is friend function } OUTPUT: Friend Classes: class x { private: int a,b; //private data members public: //initializing data members of class x void init(int u, int t) { a=u; b=t; } friend class y; //making class y as friend of x }; class y { public: int m,n; //initializing data members of class y void init(int q, int p) { m=q; n=p; } // member function of class of y is accessing private data members of class of x void print(x x1) { cout<<"adding a+m:"<<x1.a+m<<endl; cout<<"adding b+n:"<<x1.b+n<<endl; } }; int main() { x xx; xx.init(11,22); y yy; yy.init(33,44); yy.print(xx); } OUTPUT: Friend Classes with common non member function as friend: class y; //forward declaration of class y class x{ int a,b; //private data members
  • 2. 2 | P a g e A n a n d a K u m a r H N public: int e,f; //public data members //initializing data members of class x void initx(int x,int y,int o,int n) { a=x; b=y; e=o; f=n; } //making non member function as friend of class x friend void show(x x1,y y1); friend class y; //making class y as friend of class x }; class y{ int c,d; public: //initializing data members of class y void inity(int t, int u) { c=t; d=u; } //making non member function as friend of class x friend void show(x x1,y y1); }; //definition of non member of function void show(x x1,y y1) { cout<<"a:"<<x1.a<<endl; cout<<"b:"<<x1.b<<endl; cout<<"c:"<<y1.c<<endl; cout<<"d:"<<y1.d<<endl; cout<<"a+c="<<x1.a+y1.c<<endl; cout<<"b+d="<<x1.b+y1.d<<endl; } int main() { x x1; y y1; x1.initx(10,20,3,4); y1.inity(25,40); show(x1,y1); //calling friend function without object reference } OUTPUT: Passing objects as arguments: class x{ public: int a,b; void add(x x1) //reciving object as a parameter { cout<<"add:"<<x1.a+x1.b<<endl; } float avg(x); //member function declaration }; float x::avg(x x2) //defining function outside the class { return ((x2.a+x2.b)/2.0); //returning float value } int main() { x n1,n2; cout<<"enter two valuesn"; cin>>n1.a>>n1.b; n2.add(n1); //passing object n1 as parameter cout<<"avg:"<<n2.avg(n1); } OUTPUT: Returning objects: class complex{ private: int real; int img;
  • 3. 2 | P a g e A n a n d a K u m a r H N public: int e,f; //public data members //initializing data members of class x void initx(int x,int y,int o,int n) { a=x; b=y; e=o; f=n; } //making non member function as friend of class x friend void show(x x1,y y1); friend class y; //making class y as friend of class x }; class y{ int c,d; public: //initializing data members of class y void inity(int t, int u) { c=t; d=u; } //making non member function as friend of class x friend void show(x x1,y y1); }; //definition of non member of function void show(x x1,y y1) { cout<<"a:"<<x1.a<<endl; cout<<"b:"<<x1.b<<endl; cout<<"c:"<<y1.c<<endl; cout<<"d:"<<y1.d<<endl; cout<<"a+c="<<x1.a+y1.c<<endl; cout<<"b+d="<<x1.b+y1.d<<endl; } int main() { x x1; y y1; x1.initx(10,20,3,4); y1.inity(25,40); show(x1,y1); //calling friend function without object reference } OUTPUT: Passing objects as arguments: class x{ public: int a,b; void add(x x1) //reciving object as a parameter { cout<<"add:"<<x1.a+x1.b<<endl; } float avg(x); //member function declaration }; float x::avg(x x2) //defining function outside the class { return ((x2.a+x2.b)/2.0); //returning float value } int main() { x n1,n2; cout<<"enter two valuesn"; cin>>n1.a>>n1.b; n2.add(n1); //passing object n1 as parameter cout<<"avg:"<<n2.avg(n1); } OUTPUT: Returning objects: class complex{ private: int real; int img;
  • 4. 2 | P a g e A n a n d a K u m a r H N public: int e,f; //public data members //initializing data members of class x void initx(int x,int y,int o,int n) { a=x; b=y; e=o; f=n; } //making non member function as friend of class x friend void show(x x1,y y1); friend class y; //making class y as friend of class x }; class y{ int c,d; public: //initializing data members of class y void inity(int t, int u) { c=t; d=u; } //making non member function as friend of class x friend void show(x x1,y y1); }; //definition of non member of function void show(x x1,y y1) { cout<<"a:"<<x1.a<<endl; cout<<"b:"<<x1.b<<endl; cout<<"c:"<<y1.c<<endl; cout<<"d:"<<y1.d<<endl; cout<<"a+c="<<x1.a+y1.c<<endl; cout<<"b+d="<<x1.b+y1.d<<endl; } int main() { x x1; y y1; x1.initx(10,20,3,4); y1.inity(25,40); show(x1,y1); //calling friend function without object reference } OUTPUT: Passing objects as arguments: class x{ public: int a,b; void add(x x1) //reciving object as a parameter { cout<<"add:"<<x1.a+x1.b<<endl; } float avg(x); //member function declaration }; float x::avg(x x2) //defining function outside the class { return ((x2.a+x2.b)/2.0); //returning float value } int main() { x n1,n2; cout<<"enter two valuesn"; cin>>n1.a>>n1.b; n2.add(n1); //passing object n1 as parameter cout<<"avg:"<<n2.avg(n1); } OUTPUT: Returning objects: class complex{ private: int real; int img;
  • 5. 5 | P a g e A n a n d a K u m a r H N class emp { private: char name[50]; int ID; float salary; public: void read() { cout<<"enter name, ID and salaryn"; cin>>name>>ID>>salary; } void display( ) { cout<<"NAME"<<"t"<<"ID"<<"t"<<"SALARYn"; cout<<name<<"t"<<ID<<"t"<<salary<<endl; } }; int main() { emp *e; e=new emp; e->read(); e->display(); } OUTPUT: class emp { private: char name[50]; int ID; float salary; public: void read( ) { cout<<"enter name, ID and salaryn"; cin>>name>>ID>>salary; } void display( ) { cout<<name<<"t"<<ID<<"t"<<salary<<endl; } }; int main() { int sizee; cout<<"enter how many employee:"<<endl; cin>>sizee; emp *e=new emp[sizee]; for(int i=1;i<=sizee;i++) { cout<<"enter "<<i<<" employe name ID salary:"<<endl; e[i].read(); } cout<<"employe details:"<<endl<<endl; cout<<"NAME"<<"t"<<"ID"<<"t"<<"SALARY"<<e ndl; for(int i=1;i<=sizee;i++) { e[i].display(); } delete [ ] e; }
  • 6. 6 | P a g e A n a n d a K u m a r H N class student{ public: char name[30]; int usn; void read() { cin>>name>>usn; } void show() { cout<<name<<"t"<<usn<<endl; } }; int main() { student *s; s=new student[3]; for(int i=1;i<=3;i++) { cout<<"enter name and usn for student "<<i; s[i].read(); } cout<<"nametusnn"; for(int i=1;i<=3;i++) { s[i].show( ); } delete [ ] s; } OUTPUT: Pointers to objects: class emp { private: char name[50]; int ID; float salary; public: void read() { cout<<"enter name, ID and salaryn"; cin>>name>>ID>>salary; } void display() { cout<<"NAME"<<"t"<<"ID"<<"t"<<"SALARYn"; cout<<name<<"t"<<ID<<"t"<<salary<<endl; } }; int main() { emp e;
  • 7. 7 | P a g e A n a n d a K u m a r H N emp *d; d=&e; // pointer to an object; d->read(); //arrow to operator to access fuction d->display(); } class a { public: int num; a() { num=0; } a(int x) { num=x; } int get() { return num; } }; int main() { a a1(100); a a2[5]={1,2,3,4,5}; a *x,*y; x=&a1; y=&a2[0]; cout<<x->get()<<endl; for(int i=0;i<5;i++) { cout<<y->get()<<endl; y++; } } Copy constructors: class X { public: int a, b; X() { cout<<"default constructor"<<endl; } void init(int x,int y) { a=x; b=y; } X(X &m) { a=m.a; b=m.b; cout<<"copy constructor calledn"; } void show() { cout<<"a:"<<a<<endl<<"b:"<<b<<endl; } }; int main() { X x1,x2; //default constructor is called x1.init(99,88); x1.show(); x2.init(55,11);//copy constructor is called x2.show(); X x3=x1;//copy constructor is called X x4(x2);//copy constructor is called x3.show(); x4.show(); X x5; x5=x3;// just copy , copy constructor is not called x5.show(); }
  • 8. 8 | P a g e A n a n d a K u m a r H N /*All Type of CONSTRUCTORS*/ class X { public: int a, b; X( ) { a=b=0; cout<<"default constructor"<<endl; } X(int x) { a=b=x; cout<<"constructor with one parametern"; } X(int x,int y) { a=x; b=y; cout<<"constructor with two parameter"<<endl; } X(X &m) { a=m.a; b=m.b; cout<<"copy constructor calledn"; } void show() { cout<<"a:"<<a<<endl<<"b:"<<b<<endl; } }; int main() { X x1; //default constructor is called x1.show(); X x2(100);//constructor with single parameter x2.show(); X x3(29,99);//constructor with two parameter x3.show(); X x4=x3;//copy constructor is called x4.show(); X x5(x2);//copy constructor is called x5.show(); x5=x3;// just copy , copy constructor is not called x5.show(); } Generic functions: /*Generic function with ONE Type*/ template <class T1> void myfunc(T1 X) { cout<<X<<endl; } int main() { myfunc(10); myfunc("ATME-570028"); myfunc(3333.333); myfunc("3rd SEMESTER"); myfunc(0.7685); }
  • 9. 9 | P a g e A n a n d a K u m a r H N /*Generic function with TWO Type*/ template <class T1,class T2> void myfunc(T1 X,T2 Y) { cout<<X<<"tt"<<Y<<endl; } int main() { myfunc(10,25); myfunc(11.222,44.567); myfunc("ATME","570028"); myfunc(55,76.666); myfunc("atme mysore",570028); } /*Overloading Generic Fuctions*/ template <class T1> void myfunc(T1 X) { cout<<"single parameter: "<<X<<endl; } template <class T1,class T2> void myfunc(T1 X,T2 Y) { cout<<"TWO parameters: "<<X<<" "<<Y<<endl; } int main() { myfunc(10,25.555); myfunc(55,"ATME-570028"); myfunc(3333.333); myfunc("atme","mysore"); myfunc(100,200); myfunc("3rd Semester"); } /*Overriding Generic Fuction*/ template <class T1> void myfunc(T1 X) { cout<<"single parameter: "<<X<<endl; } template <class T1,class T2> void myfunc(T1 X,T2 Y) { cout<<"TWO parameter: "<<X<<" "<<Y<<endl; } void myfunc(int m,int n) { cout<<"inside fuction myfuc: "<<m<<" "<<n<<endl; } int main() { myfunc(10,25.555); //calls generic function myfunc(55,"ATME-570028"); //calls generic function myfunc(3333.333); //calls generic function myfunc(1,2); //overrides generic function myfunc("atme mysore"); //calls generic function myfunc(100,200); //overrides generic function } Generic CLASSES: /*Generic CLASS with ONE type*/ template <class T> class x { public: T a; T b;
  • 10. 10 | P a g e A n a n d a K u m a r H N x(T m,T n) { a=m; b=n; } void add() { cout<<”sum(a,b)=”<<a+b<<endl; } }; int main() { x<int>x1(111,222); x<float>x2(2.33,4.7); x<double>x3(22.33,44.77); x1.add(); x2.add(); x3.add(); } /*Generic CLASS with TWO types*/ template <class T1,class T2> class x { public: T1 a; T2 b; x(T1 m,T2 n) { a=m; b=n; } void add() { cout<<"sum(a,b)="<<a+b<<endl; } }; int main() { x<int,int>x1(11,222); x<int,float>x2(2,4.7); x<double,float>x3(33.333,55.555); x1.add(); x2.add(); x3.add(); } Operator overloading /*overloaing unary + operator as a member fuction*/ class a { public: int m,n; void init(int x, int y) { m=x; n=y; } //overloaing as member fuction; a operator+() { a t; t.m=+m; t.n=+n; return t; } void show() { cout<<m<<endl<<n<<endl<<endl; } }; int main() { a aa,cc; aa.init(-1,-1); aa.show(); cc=+aa; cc.show(); } /*overloaing unary - operator as a member fuction*/ class a { public: int m,n; void init(int x, int y) { m=x; n=y; }
  • 11. 11 | P a g e A n a n d a K u m a r H N a operator-( ) { a t; t.m=-m; t.n=-n; return t; } void show( ) { cout<<m<<endl<<n<<endl<<endl; } }; int main( ) { a aa,bb,cc; aa.init(-1,-1); bb.init(-20,-40); aa.show( ); bb.show( ); cc=-aa; cc.show( ); cc=-bb; cc.show( ); }