C++ File Grayscale
C++ File Grayscale
Output:
Practical # 2
Objective: To implement the concept of class using member
function defined outside class and passing objects as function
arguments.
// Header Files
#include<iostream.h>
#include<conio.h>
class point // Class Declaration
{
public:
int x,y;
void getdata(int x,int y);
};
void point::getdata(int a,int b)
{
x=a;
y=b;
}
int main() // Main Function
{
point p1,p2;
int p,q;
clrscr(); // Clears the screen
cout<<"Enter the value of x\n";
cin>>p;
cout<<"Enter the value of y\n";
cin>>q;
p1.getdata(p,q);
cout<<"Enter the value of x\n";
cin>>p;
cout<<"Enter the value of y\n";
cin>>q;
p2.getdata(p,q);
cout<<"The Aggregate values of x is\n"<<p1.x+p2.x;
cout<<"\nThe aggregate values of y is\n"<<p1.y+p2.y;
getch();
return 0;
}
Output:
Practical # 3
Output:
Practical # 4
Objective: To implement the concept of class using member
function defined ouside class and initialization of data members.
// Header Files
#include<iostream.h>
#include<conio.h>
class phone
{
public:
int code,exch;
char no[10];
};
int main()
{
phone p1;
clrscr();
cout<<"Plz Enter Your Area Code : ";
cin>>p1.code;
cout<<"Plz Enter your exchange code : ";
cin>>p1.exch;
cout<<"Plz Enter your number : ";
cin>>p1.no;
cout<<"Your number
is :"<<"("<<p1.code<<")"<<"0"<<p1.exch<<"-"<<p1.no;
getch();
return 0;
}
Output:
Practical # 5
Objective: To implement the use of friend function using multiple
classes.
#include<iostream.h>
#include<conio.h>
class db;
class dm
{
float meter,centi;
public:
void set1(void)
{
cout<<"Enter the distance in meter\n";
cin>>meter;
cout<<"Enter the distance in centimeter\n";
cin>>centi;
}
friend void addvalue(dm,db);
};
class db
{
float inch,feet;
public:
void set2(void)
{
cout<<"Enter distance in feet\n";
cin>>feet;
cout<<"Enter distance in inches\n";
cin>>inch;
}
friend void addvalue(dm,db);
};
void addvalue(dm m, db b)
{
cout<<"Result in
meters\n"<<m.meter+m.centi/100+b.feet/3.28+b.inch/39.37;
}
int main()
{
dm ab;
db xy;
clrscr();
ab.set1();
xy.set2();
addvalue(ab,xy);
getch();
return 0;
}
Output:
Practical # 6
Output:
Practical # 7
Output:
Practical # 8
Output:
Practical # 9
Output:
Practical # 11
Output:
Practical # 12
#include<iostream.h>
#include<conio.h>
class base
{
public:
void display()
{
cout<<"Display Base\n";
}
virtual void show()
{
cout<<"Show Base\n";
}
};
class derived:public base
{
public:
void display()
{
cout<<"Display Derived\n";
}
void show()
{
cout<<"Show Derived\n";
}
};
int main()
{
base B;
derived D;
base *ptr;
clrscr();
cout<<"\n\n******** Virtual Function ********\n";
cout<<"\nPointer points to Base\n";
ptr=&B;
ptr->display(); // Calls Base Version
ptr->show(); // Calls Base Version
cout<<"\nPointer points to Derived\n";
ptr=&D;
ptr->display(); // Calls Base Version
ptr->show(); // Calls Derived Version
getch();
return 0;
}
Output:
Practical # 13
a=x;
b=y;
}
void show()
{
cout<<a<<" and "<<b<<endl;
}
};
int main()
{
clrscr();
test<float,int> test1(1.23,123);
test<int,char> test2(100,'W');
test1.show();
test2.show();
getch();
return 0;
}
Output:
Practical # 15
Objective: To implement the concept of Exception Handling.
#include<iostream.h>
#include<conio.h>
void divide(int x,int y,int z)
{
cout<<"\nWe are inside the function\n";
if((x-y)!=0) // It is OK
{
int R=z/(x-y);
cout<<"Result = "<<R<<endl;
}
else // There is a problem
{
throw(x-y); // Throw point
}
}
int main()
{
try
{
cout<<"We are in the try block\n";
divide(10,20,30); // Invoke divide
divide(10,10,20); // Invoke divide
}
catch(int i) // Catches the exception
{
cout<<"Caught the exception\n";
}
getch();
return 0;
}