SlideShare a Scribd company logo
1 of 39
1
2
3
4
Friend Function
5
6
Example
class Sum
{
private:
int a, b;
public:
void get_num()
{
cout<<“Enter any two numbers:n”;
cin>>a>>b;
}
friend int add(); //Friend function
Declaration
};
int add() // Friend function Defination
{
Sum s;
int temp;
s.get_num();
temp=s.a+s.b; // accessing private data
return temp;
}
int main()
{
int result;
result=add();
cout<<“Sum=”<<result<<endl;
}
Output
Enter any two numbers:
7
3
Sum= 10
7
8
9
10
Example
class B;
classA
{
private:
int a;
public:
void setdata(int x)
{a=x;}
friend void fun(A,B);
};
class B
{
private:
int b;
public:
void setdata(int y)
{b=y;}
friend void fun(A,B);
};
void fun(A o1,B o2)
{
cout<<“sum is:”<<o1.a+o2.b;
}
void main()
{
A obj1;
B obj2;
obj1.setdata(2);
obj2.setdata(7);
fun(obj1,obj2);
}
Output
Sum is: 9
11
12
13
Example
class example
{
private:
static int sum; //Static data
int x;
public:
example() //Constructor of the class
{
sum=sum+1;
x=sum;
}
~example() //Destructor of the class
{
sum=sum-1;
}
static void exforsys()
//Static function exforsys( ) defined with keyword
static
{
cout << "nResult is: " << sum;
}
void number() //Normal member
function number( )
{
cout << "nNumber is: " << x;
}
};
int example::sum=0;
void main()
{
example e1;
example::exforsys();
//Static function exforsys() accessed
using class name example and the scope
resolution operator ::
example e2,e3,e4;
example::exforsys();
e1.number();
14
//Normal member function accessed using
object e1 and the dot member access
operator.
e2.number();
e3.number();
e4.number();
}
OUTPUT:
Result is:1
Result is:4
Number is:1
Number is:2
Number is:3
Number is:4
15
16
17
Example
using namespace std;
class Box{
private:
int l,b,h;
public:
void setDimension(int l,int b,int h)
{
this->l=l;
this->b=b;
this->h=h;
}
void showDimension()
{
cout<<"nl="<<l<<" b="<<b<<"
h="<<h;
}
};
int main(){
Box smallBox;
smallBox.setDimension(12,10,5);
smallBox.showDimension();
}
Output
l=12 b=10 h=5
18
"many-shaped”.
many forms
19
20
21
Example
#include<iostream>
using namespace std;
class shape{
public:
int height,width;
float area;
shape(){
height=5;width=8;
void area();
};
class triangle:shape{
public:
void area(){
area=2*height;
cout<<area;
};
class rectangle:shape{
public:
void area(){
area=height*width;
cout<<area;}
};
int main(){
triangle a;
rectangle b;
a.area();
b.area();
}
22
23
24
25
26
Syntax:
return type class name :: operator op
(argument list)
{
Body of function
}
27
Example
#include <iostream>
using namespace std;
classTest
{
private:
int count;
public:
Test(): count(5)
{ }
void operator ++()
{
count = count+1;
}
void Display()
{
cout<<"Count: "<<count;
}
};
int main()
{
Test t; // this calls "function void operator
++()" function
++t;
t.Display();
return 0;
}
28
Example
#include<iostream>
using namespace std;
class num{
private:
int a,b,c,d;
public:
void input(void);
void show(void);
num operator+(num);
};
void num::input(){
cout<<“enter values for a,b,c,d:”;
cin>>a>>b>>c>>d;
}
void num::show(){
cout<<“a”<<a<<“b”<<b<<“c”<<c<<“d”<<d
;
}
num num::operator +(num t){
num tmp;
tmp.a=a+t.a;
tmp.b=b+t.b;
tmp.c=c+t.c;
tmp.d=d+t.d;
return (temp);
}
int main(){
num X,Y,Z;
cout<<“/n object X:”;
X.input();
cout<<“/n objectY:”;
29
Y.input();
Z=X+Y;
cout<<“/n X:”;
X.show();
cout<<“/nY:”;
Y.show();
cout<<“/n z:”;
Z.show();
}
Output
Object X
Enter values for a b c d:1 4 2 1
ObjectY
Enter values for a b c d:2 5 4 2
X:a=1 b=4 c=2 d=1
Y:a=2 b=5 c=4 d=2
Z:a=3 b=9 c=6 d=3
30
class num{
private:
int a,b,c,d;
public:
void input(void);
void show(void);
friend num operator*(int,num);
};
void num::input(){
cout<<“enter values for a,b,c,d:”;
cin>>a>>b>>c>>d;
}
void num::show(){
cout<<“a”<<a<<“b”<<b<<“c”<<c<<“d”<<d
;
}
num num::operator *(int a,num t){
num tmp;
tmp.a=a*t.a;
tmp.b=b*t.b;
tmp.c=c*t.c;
tmp.d=d*t.d;
return (temp);
}
int main(){
Num X,Z;
cout<<“/n object X:”;
X.input();
Z=3*X;
cout<<“/n X:”;
X.show();
cout<<“/n Z:”;
Z.show();}
Output
Object X
Enter values for a b c d:1 2 2 3
X:a=1 b=2 c=2 d=3
Y:a=3 b=6 c=6 d=9
31
method overloading "sum()"
32
33
Example
#include<iostream>
#include<conio.h>
class Addition
{
public:
void sum(int a, int b)
{
cout<<a+b;
}
void sum(int a, int b, int c)
{
cout<<a+b+c; }
};
main()
{
Addition obj;
obj.sum(10, 20);
cout<<endl;
obj.sum(10, 20, 30); }
Output
30
60
34
35
Syntax:
class classname{
public :
virtual void
memberfunctionname ( ) //
this denotes the c++virtual
function
{ : }
};
Example
class vehicle
//base class of C++ virtual function
{
public:
virtual void make ( ) // C++ virtual
function
{ cout<<" member function of base class
vehicle accessed" <<endl;}
};
class fourwheeler : public vehicle
{
public:
void make ( )
{
cout <<" virtual member function of derived
class fourwheeler accessed"<< endl;}
};
int main ( )
{
vehicle *a, *b;
a = new vehicle ( );
a-> make ( );
b = new fourwheeler ( );
b-> make ( );
return 0;}
Output:
Member function of base class
vehicle accessed
virtual member function of derived
class fourwheeler accessed.
36
37
38
Example
#include<iostream.h>
#include<conio.h>
class Base {
public:
void show() {
cout<<"Base class"; }
};
class Derived:public Base {
public: void show() {
cout<<"Derived Class"; }
};
int mian() {
Base b;
Derived d;
b.show();
d.show();
}
Output
Base class Derived Class
39

More Related Content

What's hot

Travel management
Travel managementTravel management
Travel management1Parimal2
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++Bharat Kalia
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st StudyChris Ohk
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th StudyChris Ohk
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphismmohamed sikander
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th StudyChris Ohk
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Syed Umair
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 

What's hot (20)

C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
C++ TUTORIAL 6
C++ TUTORIAL 6C++ TUTORIAL 6
C++ TUTORIAL 6
 
C++ TUTORIAL 7
C++ TUTORIAL 7C++ TUTORIAL 7
C++ TUTORIAL 7
 
C++ programs
C++ programsC++ programs
C++ programs
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Travel management
Travel managementTravel management
Travel management
 
Basic Programs of C++
Basic Programs of C++Basic Programs of C++
Basic Programs of C++
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Static and const members
Static and const membersStatic and const members
Static and const members
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
Inheritance and polymorphism
Inheritance and polymorphismInheritance and polymorphism
Inheritance and polymorphism
 
Oop1
Oop1Oop1
Oop1
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
Pratik Bakane C++
Pratik Bakane C++Pratik Bakane C++
Pratik Bakane C++
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)Assignement of programming & problem solving u.s ass.(1)
Assignement of programming & problem solving u.s ass.(1)
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Implementing stack
Implementing stackImplementing stack
Implementing stack
 

Similar to New presentation oop

Presentation on template and exception
Presentation  on template and exceptionPresentation  on template and exception
Presentation on template and exceptionSajid Alee Mosavi
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and ObjectsPayel Guria
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...MaruMengesha
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical fileMitul Patel
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++Abhishek Pratap
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsAlfonso Peletier
 
Assignement c++
Assignement c++Assignement c++
Assignement c++Syed Umair
 

Similar to New presentation oop (20)

C++ prgms 3rd unit
C++ prgms 3rd unitC++ prgms 3rd unit
C++ prgms 3rd unit
 
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
 
More on Classes and Objects
More on Classes and ObjectsMore on Classes and Objects
More on Classes and Objects
 
12
1212
12
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
WINSEM2020-21_STS3105_SS_VL2020210500169_Reference_Material_I_04-Mar-2021_L14...
 
Java programs
Java programsJava programs
Java programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 
Hybrid Inheritance in C++
Hybrid Inheritance in C++Hybrid Inheritance in C++
Hybrid Inheritance in C++
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Qno 1 (d)
Qno 1 (d)Qno 1 (d)
Qno 1 (d)
 
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic LabsTypeScript - All you ever wanted to know - Tech Talk by Epic Labs
TypeScript - All you ever wanted to know - Tech Talk by Epic Labs
 
662305 10
662305 10662305 10
662305 10
 
Assignement c++
Assignement c++Assignement c++
Assignement c++
 

Recently uploaded

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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
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
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 

Recently uploaded (20)

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
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
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
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
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 🔝✔️✔️
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 

New presentation oop

  • 1. 1
  • 2. 2
  • 3. 3
  • 4. 4
  • 6. 6 Example class Sum { private: int a, b; public: void get_num() { cout<<“Enter any two numbers:n”; cin>>a>>b; } friend int add(); //Friend function Declaration }; int add() // Friend function Defination { Sum s; int temp; s.get_num(); temp=s.a+s.b; // accessing private data return temp; } int main() { int result; result=add(); cout<<“Sum=”<<result<<endl; } Output Enter any two numbers: 7 3 Sum= 10
  • 7. 7
  • 8. 8
  • 9. 9
  • 10. 10 Example class B; classA { private: int a; public: void setdata(int x) {a=x;} friend void fun(A,B); }; class B { private: int b; public: void setdata(int y) {b=y;} friend void fun(A,B); }; void fun(A o1,B o2) { cout<<“sum is:”<<o1.a+o2.b; } void main() { A obj1; B obj2; obj1.setdata(2); obj2.setdata(7); fun(obj1,obj2); } Output Sum is: 9
  • 11. 11
  • 12. 12
  • 13. 13 Example class example { private: static int sum; //Static data int x; public: example() //Constructor of the class { sum=sum+1; x=sum; } ~example() //Destructor of the class { sum=sum-1; } static void exforsys() //Static function exforsys( ) defined with keyword static { cout << "nResult is: " << sum; } void number() //Normal member function number( ) { cout << "nNumber is: " << x; } }; int example::sum=0; void main() { example e1; example::exforsys(); //Static function exforsys() accessed using class name example and the scope resolution operator :: example e2,e3,e4; example::exforsys(); e1.number();
  • 14. 14 //Normal member function accessed using object e1 and the dot member access operator. e2.number(); e3.number(); e4.number(); } OUTPUT: Result is:1 Result is:4 Number is:1 Number is:2 Number is:3 Number is:4
  • 15. 15
  • 16. 16
  • 17. 17 Example using namespace std; class Box{ private: int l,b,h; public: void setDimension(int l,int b,int h) { this->l=l; this->b=b; this->h=h; } void showDimension() { cout<<"nl="<<l<<" b="<<b<<" h="<<h; } }; int main(){ Box smallBox; smallBox.setDimension(12,10,5); smallBox.showDimension(); } Output l=12 b=10 h=5
  • 18. 18
  • 20. 20
  • 21. 21 Example #include<iostream> using namespace std; class shape{ public: int height,width; float area; shape(){ height=5;width=8; void area(); }; class triangle:shape{ public: void area(){ area=2*height; cout<<area; }; class rectangle:shape{ public: void area(){ area=height*width; cout<<area;} }; int main(){ triangle a; rectangle b; a.area(); b.area(); }
  • 22. 22
  • 23. 23
  • 24. 24
  • 25. 25
  • 26. 26 Syntax: return type class name :: operator op (argument list) { Body of function }
  • 27. 27 Example #include <iostream> using namespace std; classTest { private: int count; public: Test(): count(5) { } void operator ++() { count = count+1; } void Display() { cout<<"Count: "<<count; } }; int main() { Test t; // this calls "function void operator ++()" function ++t; t.Display(); return 0; }
  • 28. 28 Example #include<iostream> using namespace std; class num{ private: int a,b,c,d; public: void input(void); void show(void); num operator+(num); }; void num::input(){ cout<<“enter values for a,b,c,d:”; cin>>a>>b>>c>>d; } void num::show(){ cout<<“a”<<a<<“b”<<b<<“c”<<c<<“d”<<d ; } num num::operator +(num t){ num tmp; tmp.a=a+t.a; tmp.b=b+t.b; tmp.c=c+t.c; tmp.d=d+t.d; return (temp); } int main(){ num X,Y,Z; cout<<“/n object X:”; X.input(); cout<<“/n objectY:”;
  • 29. 29 Y.input(); Z=X+Y; cout<<“/n X:”; X.show(); cout<<“/nY:”; Y.show(); cout<<“/n z:”; Z.show(); } Output Object X Enter values for a b c d:1 4 2 1 ObjectY Enter values for a b c d:2 5 4 2 X:a=1 b=4 c=2 d=1 Y:a=2 b=5 c=4 d=2 Z:a=3 b=9 c=6 d=3
  • 30. 30 class num{ private: int a,b,c,d; public: void input(void); void show(void); friend num operator*(int,num); }; void num::input(){ cout<<“enter values for a,b,c,d:”; cin>>a>>b>>c>>d; } void num::show(){ cout<<“a”<<a<<“b”<<b<<“c”<<c<<“d”<<d ; } num num::operator *(int a,num t){ num tmp; tmp.a=a*t.a; tmp.b=b*t.b; tmp.c=c*t.c; tmp.d=d*t.d; return (temp); } int main(){ Num X,Z; cout<<“/n object X:”; X.input(); Z=3*X; cout<<“/n X:”; X.show(); cout<<“/n Z:”; Z.show();} Output Object X Enter values for a b c d:1 2 2 3 X:a=1 b=2 c=2 d=3 Y:a=3 b=6 c=6 d=9
  • 31. 31
  • 33. 33 Example #include<iostream> #include<conio.h> class Addition { public: void sum(int a, int b) { cout<<a+b; } void sum(int a, int b, int c) { cout<<a+b+c; } }; main() { Addition obj; obj.sum(10, 20); cout<<endl; obj.sum(10, 20, 30); } Output 30 60
  • 34. 34
  • 35. 35 Syntax: class classname{ public : virtual void memberfunctionname ( ) // this denotes the c++virtual function { : } };
  • 36. Example class vehicle //base class of C++ virtual function { public: virtual void make ( ) // C++ virtual function { cout<<" member function of base class vehicle accessed" <<endl;} }; class fourwheeler : public vehicle { public: void make ( ) { cout <<" virtual member function of derived class fourwheeler accessed"<< endl;} }; int main ( ) { vehicle *a, *b; a = new vehicle ( ); a-> make ( ); b = new fourwheeler ( ); b-> make ( ); return 0;} Output: Member function of base class vehicle accessed virtual member function of derived class fourwheeler accessed. 36
  • 37. 37
  • 38. 38 Example #include<iostream.h> #include<conio.h> class Base { public: void show() { cout<<"Base class"; } }; class Derived:public Base { public: void show() { cout<<"Derived Class"; } }; int mian() { Base b; Derived d; b.show(); d.show(); } Output Base class Derived Class
  • 39. 39