CPP Word File
CPP Word File
a. Write a friend function for adding the two complex numbers, using a single class
Code:
#include<iostream.h>
#include<conio.h>
{ {
private: complex t;
public: t.img=a.img+b.img;
{ }
} {
complex a,b,c;
{ b.set();
Output
b. Write a friend function for adding the two different distances and display its sum, using two classes.
Code:
#include<conio.h> {
private: {
public: dis.m=(d1.km+d2.km)*1000;
{ }
cin>>km; {
} clrscr();
d1.getdata();
{ con=conv(d1,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 {
private: cin>>a>>b;
void dispdata() {
{ clrscr();
} }
};
Output:
b. Design a class Geometry containing the methods area() and volume() and also overload the area() function .
Code:
#include<conio.h> {
private: }
float ac,vc,r; };
geometry() {
{ clrscr();
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
Code:-
#include<conio.h> {
{ y=-y;
private: z=-z;
int x,y,z; }
public: };
{ {
x=a; clrscr();
} s.dispdata();
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()
cin>>nm;
void operator+(string s)
strcat(nm,s.nm);
};
void main()
clrscr();
string s1,s2;
s1.getdata();
s2.getdata();
s1+s2;
getch();
Output:
7. String handling
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
private:
char nm[50];
public:
void getdata()
void operator+(string s)
strcat(nm,s.nm);
};
void main()
clrscr();
string s1,s2;
s1.getdata();
s2.getdata();
s1+s2;
getch();
Output:
Code:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
private:
char nm[50];
public:
void getdata()
cin>>nm;
void operator<(string s)
if(strcmp(nm,s.nm)<0)
else
};
void main()
clrscr();
string s1,s2;
s1.getdata();
s2.getdata();
s1<s2;
getch();
Output:
c. Console formatting functions.
8. Exception handling
Code:
#include<iostream.h> }
#include<conio.h> else
void main() {
{ throw b;
int a,b; }
cin>>a>>b; catch(int i)
try {
if(b>0) }
{ getch();
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.