際際滷

際際滷Share a Scribd company logo
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

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