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( );
}

C++ prgms 3rd unit

  • 1.
    1 | Pa 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 | Pa 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 | Pa 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 | Pa 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 | Pa 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 | Pa 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 | Pa 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 | Pa 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 | Pa 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 | Pa 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 | Pa 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( ); }