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;

{ }

} {

complex a,b,c;

{ 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;

{ }

cin>>km; {

} clrscr();

d1.getdata();

## void display() d2.getdata();

{ con=conv(d1,d2);

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.

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;

} void main()

void dispdata() {

{ clrscr();

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

} }

};

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> {

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

private: }

float ac,vc,r; };

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.

Code:-

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

#include<conio.h> {

{ y=-y;

private: z=-z;

int x,y,z; }

public: };

{ {

x=a; clrscr();

} s.dispdata();

## void dispdata() -s;

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

};

void main()

clrscr();

string s1,s2;

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

s1+s2;

getch();

Output:

## 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);

};

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)

else

## 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 {

if(b>0) }

{ getch();

## 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.