You are on page 1of 9

2. Using friend functions.

a. Write a friend function for adding the two complex numbers, using a single class

Code:

#include<iostream.h>

#include<conio.h>

class complex complex sum(complex a,complex b)

{ {

private: complex t;

int real,img; t.real=a.real+b.real;

public: t.img=a.img+b.img;

void set() return t;

{ }

cout<<"Enter real and img part ";

cin>>real>>img; void main()

} {

friend complex sum(complex,complex); clrscr();

complex a,b,c;

void display() a.set();

{ b.set();

cout<<"\n\nThe sum of complex num is: c=sum(a,b);


"<<real<<"+"<<img<<"i";
c.display();
}
getch();
};
}

Output

b. Write a friend function for adding the two different distances and display its sum, using two classes.

Code:

#include<iostream.h> class distance

#include<conio.h> {
private: {

int km,m; distance dis;

public: dis.m=(d1.km+d2.km)*1000;

void getdata() return dis;

{ }

cout<<"\n Enter distance in km : "; void main()

cin>>km; {

} clrscr();

friend distance conv(distance,distance) distance d1,con,d2;

d1.getdata();

void display() d2.getdata();

{ con=conv(d1,d2);

cout<<"\n Meter converted Addition of two distance con.display();


is : "<<m;
getch();
}
}
};

distance conv(distance d1,distance d2)

Output:

c. Write a friend function for adding the two matrix from two different classes and display its sum.

3. Constructors and method overloading.

a. Design a class Complex for adding the two complex numbers and also show the use of constructor.

Code:

#include<iostream.h> public:

#include<conio.h> complex()

class complex {

{ cout<<"\n Enter two numbers";

private: cin>>a>>b;

int a,b,add; add=a+b;


} void main()

void dispdata() {

{ clrscr();

cout<<"\n \n 1st number :"<<a; complex s;

cout<<"\n \n 2nd number :"<<b; s.dispdata();

cout<<"\n \n Addition of two numbers :"<<add; getch();

} }

};

Output:

b. Design a class Geometry containing the methods area() and volume() and also overload the area() function .

Code:

#include<iostream.h> void dispdata()

#include<conio.h> {

class geometry cout<<"\n Area of Circle : "<<ac;

{ cout<<"\n \n Volume of Circle : "<<vc;

private: }

float ac,vc,r; };

public: void main()

geometry() {

{ clrscr();

cout<<"\n \n enter radius of a circle"; geometry g;

cin>>r; g.dispdata();

ac=3.14*r*r; getch();

vc=3.14*r*r*r; }

}
Output:

c. Design a class StaticDemo to show the implementation of static variable and static function. 4.

5.Operator Overloading

a. Overload the operator unary(-) for demonstrating operator overloading.

Code:-

#include<iostream.h> void operator-()

#include<conio.h> {

class space x=-x;

{ y=-y;

private: z=-z;

int x,y,z; }

public: };

space(int a,int b,int c) void main()

{ {

x=a; clrscr();

y=b; space s(100,-200,300);

z=c; cout<<"\n Before overloading :";

} s.dispdata();

void dispdata() -s;

{ cout<<"\n After overloading :";

cout<<"\n"<<x; s.dispdata();

cout<<"\n"<<y; getch();

cout<<"\n"<<z; }

Output:-
b. Overload the operator + for adding the timings of two clocks, And also pass objects as an argument.

c. Overload the + for concatenating the two strings. For e.g “Py” + “thon” = Python

Code:

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

private:

char nm[50];

public:

void getdata()

cout<<"\n Enter string";

cin>>nm;

void operator+(string s)

strcat(nm,s.nm);

cout<<"\n string is:--"<<nm;

};

void main()

clrscr();

string s1,s2;

s1.getdata();
s2.getdata();

s1+s2;

getch();

Output:

6. Virtual functions and abstract classes

a. Implement the concept of method overriding.

b. Show the use of virtual function

c. Show the implementation of abstract class.

7. String handling

a. String operations for string length , string concatenation

Code:

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

private:

char nm[50];

public:

void getdata()

cout<<"\n Enter any string:--";


cin>>nm;

void operator+(string s)

strcat(nm,s.nm);

cout<<"\n string is:--"<<nm;

cout<<"\n\n String length is:--"<<strlen(nm);

};

void main()

clrscr();

string s1,s2;

s1.getdata();

s2.getdata();

s1+s2;

getch();

Output:

b. String operations for string reverse, string comparison,

Code:

#include<iostream.h>

#include<conio.h>

#include<string.h>

class string

private:
char nm[50];

public:

void getdata()

cout<<"\n Enter any string :--";

cin>>nm;

void operator<(string s)

if(strcmp(nm,s.nm)<0)

cout<<"\n First string is smaller";

else

cout<<"\n Second string is smaller";

cout<<"\n\n REverse First string:-- "<<strrev(nm);

cout<<"\n\n Reverse second string:--"<<strrev(s.nm);

};

void main()

clrscr();

string s1,s2;

s1.getdata();

s2.getdata();

s1<s2;

getch();

Output:
c. Console formatting functions.

8. Exception handling

a. Show the implementation of exception handling

Code:

#include<iostream.h> }

#include<conio.h> else

void main() {

{ throw b;

int a,b; }

cout<<"\n Enter the value of a & b"; }

cin>>a>>b; catch(int i)

try {

{ cout<<"\n Caught exception"<<i;

if(b>0) }

{ getch();

cout<<"\n division of a & b"<<a/b; }

b. Show the implementation for exception handling for strings

c. Show the implementation of exception handling for using the pointers.

9. File handling

a. Design a class File Demo open a file in read mode and display the total number of words and lines in the file.

b. Design a class to handle multiple files and file operations.

c. Design a editor for appending and editing the files.