C++ Program

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 31

INDEX

Ex .No DATE NAME OF THE EXPERIMENTS PAGE No.


.
1 Function Overloading, Default Arguments and Inline
Function.
2 Class and Objects.

Passing Objects To Functions.


3
Friend Function.
4
Constructors and Destructors.
5
Unary Operator Overloading.
6

Binary Operator Overloading.


7

8 Inheritance.
Single, MultiLevel, Multiple,Hierarchical
Inheritance.

9 Virtual Functions.

10 Manipulate a Text File.

11 To find the Biggest Number using Command Line


Argument.

12 Sequential I/O Operations on a File

13 Class Template.

14 Function Template

15 Exception Handling.

Faculty In charge
[Mrs.D.AISHWARYA]
EX.NO: 1 (A)
FUNCTION OVERLOADING
PROGRAM
#include<iostream.h>
#include<conio.h>
#define pi 3.14
class obj
{
public:
void volume(int a);
void volume(float r);
void volume(float r,float h);
void volume(float r,int h);
};
void obj::volume(int a)
{
int v;
v=a*a*a;
cout<<"\n The Volume of Cube is:"<<v;
}
void obj::volume(float r)
{
float v;
v=4/3.0*pi*r*r*r;
cout<<"\n The Volume of Sphere is :"<<v;
}
void obj::volume(float r,float h)
{
float v;
v=1/3.0*pi*r*r*h;
cout<<"\n The Volume of Cone is:"<<v;
}
void obj::volume(float r,int h)
{
float v;
v=pi*r*r*h;
cout<<"\n The Volume of Cylinder is:"<<v;
}
void main()
{
clrscr();
obj o;
int a,ch;
float r,h;
cout<<"\n Output\n";
do
{
cout<<"\n 1.Cube\n 2.Sphere\n 3.Cone\n 4.Cylinder\n 5.Exit";
cout<<"\n Enter your choice";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n Enter the side of cube in integer";
cin>>a;
o.volume(a);
break;
case 2:
cout<<"\n Enter the radius of sphere in float";
cin>>r;
o.volume(r);
break;
case 3:
cout<<"\n Enter the radius and height of cone in float";
cin>>r>>h;
o.volume(r,h);
break;
case 4:
cout<<"\n Enter the radius of cylinder in float";
cin>>r;
cout<<"\n Enter the height of cylinder in integer";
cin>>h;
o.volume(r,h);
break;
case 5:
break;
default:
cout<<"\n wrong choice";
}
}
while(ch!=5);
getch();
}
OUTPUT
EX.NO:1(B)
INLINE FUNCTION
PROGRAM
#include<iostream.h>
inline displayNum(int m)
{
return m;
cout<<m<<endln;
}
int main( )
{
Cout<<”displayNum(5)”;
Cout<<”displayNum(8)”;
Cout<<”displayNum(666)”;
return 0;
}

OUTPUT

display(5) 666
EX.NO:1(C)
DEFAULT ARGUMENTS
PROGRAM
#include<iostream.h>
int Sum(int x,int y,int z=0,w=0)
{
return (x+y+z+w);
}
int main( )
{
Cout<<”sum(10,15)”<<endl;
Cout<<”sum(10,15,25)”<<endl;
Cout<<”sum(10,15,25,30)”<<endl;
return 0;
}

OUTPUT

25
50
80
EX.NO:2
CLASS AND OBJECTS
PROGRAM
#include<iostream.h>
class Room
{
Public:
double length;
double breath;
double height;
double calculateArea( )
{
return length*breath;
}
double calculateVolume( )
{
return length*breath*height;
}

int main( )
{
Room room1;
room1.length=42.5;
room1.breath=30.8;
room1.height=30.8;
cout<<”Area of room=”<<room1.calculateArea( )<<endl;
cout<<”Volume of room=”<<room1.calculateVolume( )<<endl;
return 0;
}

OUTPUT

Area of room=1309
Volume of room=25132.8
EX.NO:3
PASSING OBJECTS TO FUNCTIONS
PROGRAM
#include<iostream.h>
class student
{
Public:
double marks;
student(double m)
{
marks=m;
}
};
void calculate average(student s1,student s2)
{
double average=(s1.marks+s2.marks)/2;
cout<<”Average marks=”<< average<<endl;
}
int main( )
{
Student student1(88.0), student1(56.0);
Calculateaverage(student1,student2);
}

OUTPUT:

Average marks
Student1 : 88.0
Student2 : 56.0
EX.NO: 4

FRIEND FUNCTION
PROGRAM
#include <iostream.h>
class A
{
int a;
public:
A() { a = 0; }
// global friend function
friend void showA(A&);
};
void showA(A& x)
{
// Since showA() is a friend, it can access
// private members of A
std::cout << "A::a=" << x.a;
}
int main()
{
A a;
showA(a);
return 0;
}

OUTPUT
EX.NO:5
CONSTRUCTORS AND DESTRUCTORS
PROGRAM
#include<iostream.h>
#include<conio.h>
class test
{
private:
int x;
public:

test(int x1)
{
cout<<"\n Parameterized Constructor";
x = x1;
}
test(const test &t2)
{
cout<<"\n Copy Constructor";
x=t2.x;
}
int getx()
{
cout<<"\n Normal Function";
return x;
}
~test()
{
cout<<"\n Destructor Function";
}
};
int main()
{
test t1(7);
test t2 = t1;
cout<< "\n t1.x = " << t1.getx();
cout<< "\n t2.x = " << t2.getx();
getch();
return 0;
}
OUTPUT
EX.NO: 6
UNARY OPERATOR OVERLOADING
PROGRAM

#include <iostream.h>
#include<conio.h>
class Number
{
private:
int x;
public:
Number(int p) { x = p; }
void operator -() { x = -x; }
void display() { cout<<"x = "<<x; }
};
int main()
{
clrscr();
Number n(10);
-n;
n.display();
return 0;
}

OUTPUT
EX.NO: 7
BINARY OPERATOR OVERLOADING

PROGRAM

#include <iostream.h>
#include<conio.h>
class Complex
{
private:
float real;
float imag;
public:
Complex(){}
Complex(float r, float i)
{
real = r;
imag = i;
}
void display()
{
cout<<real<<"+i"<<imag;
}
friend Complex operator +(Complex &, Complex &);
};
Complex operator +(Complex &c1, Complex &c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
int main()
{
clrscr();
Complex c1(3, 4);
Complex c2(4, 6);
Complex c3 = c1+c2;
c3.display();
return 0;
}
OUTPUT
CONCEPT OF SINGLE INHERITANCE
EX:NO:8(A)
PROGRAM
#include<iostream.h>
#include<conio.h>
class vehicle {
int no;
char name[10];
public:
void getdata() {
cout<<"Enter no,Name:";
cin>>no>>name;
cout<<"\n\tvehicle Details";
cout<<"\n\tNo:"<<no<<"\n\tName:"<<name; }
};
class bike: public vehicle
{
public:
void display()
{
cout<<"\n\tprice:"<<55000;
}
};
void main()
{
clrscr();
bike b;
b.getdata();
b.display();
getch();
}
OUTPUT
EX.NO: 8 (B)
CONCEPT OF MULTILEVEL INHERITANCE
PROGRAM
#include<iostream.h>
Class A
{
Public:
int a;
void get_A_data( )
{
Cout<<”Enter values of a:”;
Cin>>a;
}
};
Class B:public A
{
Public:
int b;
void get_B_data( )
{
Cout<<”Enter values of b:”;
Cin>>b;
}
};
Class c:public B
{
void get_C_data( )
{
Cout<<”Enter values of c:”;
Cin>>c;
}
void sum( )
{
int ans=a+b=c;
cout<<”sum:”<<ans;
}
};
int main( )
{
C obj;
obj. void get_A_data( );
obj. void get_B_data( );
obj. void get_C_data( );
obj.sum( );
return 0;
}
OUTPUT

Enter value of a:4


Enter value of b:5
Enter values of c:9
Sum:18
EX.NO: 8 (C)
CONCEPT OF MULTIPLE INHERITANCE
PROGRAM
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rno,m1,m2;
public:
void get()
{
cout<<"Enter the Roll no:";
cin>>rno;
cout<<"Enter the test Mark:";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
public:
void getsm()
{
cout<<"Enter the sports mark:";
cin>>sm;
}
};
class statement:publicstudent,public sports
{
int tot,avg;
public:
void display()
{
tot=(m1+m2+sm);
avg=tot/3;
cout<<"\n\tRollno :"<<rno<<"\n\tTotal :"<<tot;
cout<<"\n\tAverage :"<<avg;
}
};
void main()
{
clrscr();
statement obj;
obj.get();
obj.getsm();
obj.display();
getch();
}

OUTPUT
EX.NO: 8 (D)
CONCEPT OF HIERARCHICAL INHERITANCE
PROGRAM
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a,b;
void getnumber()
{
cout<<"\n\nEnter Number=\t";
cin>>a;
}
};
class B:public A
{
public:
void square()
{
getnumber();
cout<<"\n\n\tSquare of the Number=\t"<<(a*a);
}
};
class C : public A
{
public:
void cube()
{
getnumber();
cout<<"\n\n\tCube of the Number=\t"<<(a*a*a);
}
};
void main()
{
clrscr();
B b1;
b1.square();
C c1;
c1.cube();
getch();
}
OUTPUT
EX.NO: 9
VIRTUAL FUNCTION
PROGRAM
#include <iostream.h>
#include<conio.h>
class base {
public:
virtual void print()
{
cout<< "print base class" <<endl;
}
void show()
{
cout<< "show base class" <<endl;
}
};
class derived : public base {
public:
void print()
{
cout<< "print derived class" <<endl;
}
void show()
{
cout<< "show derived class" <<endl;
}
};
int main()
{
clrscr();
base* bptr;
derived d;
bptr = &d;
// virtual function, binded at runtime
bptr->print();
// Non-virtual function, binded at compile time
bptr->show();
}
OUTPUT
EX:NO:10
MANIPULATE A TEXT FILE

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
fstream file; //object of fstream class

//opening file "sample.txt" in out(write) mode


file.open("sample.txt",ios::out);

if(!file)
{
cout<<"Error in creating file!!!"<<endl;
return 0;
}

cout<<"File created successfully."<<endl;


//write text into file
file<<"ABCD.";
//closing the file
file.close();

//again open file in read mode


file.open("sample.txt",ios::in);

if(!file)
{
cout<<"Error in opening file!!!"<<endl;
return 0;
}

//read untill end of file is not found.


char ch; //to read single character
cout<<"File content: ";

while(!file.eof())
{
file>>ch; //read single character from file
cout<<ch;
}
file.close(); //close file

return 0;
}

OUTPUT

File created successfully.


File content: ABCD.
EX NO 11

SEQUENTIAL I/O OPERATIONS ON A FILE

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char fname[20], ch;
ifstream fin;

cout << "Enter the name of the file: ";


cin.get(fname, 20);
cin.get(ch);

fin.open(fname, ios::in);
if (!fin)
{
cout << "An error occurred in opening the file.\n";
return 0;
}

while (fin)
{
fin.get(ch);
cout << ch;
}

return 0;
}

OUTPUT

Enter the name of the file:sample


An error occurred in opening the file
EX NO 12

TO FIND THE BIGGEST NO USING COMMAND LINE ARGUMENTS

PROGRAM

#include<iostream.h>
int main(int argc,char *argv[])
{
int c[10];
int I,temp,j,greatest;
j=0;
for(i=1,i<argc;i++)
{
temp=atoi(argv[i]);
C[j]=temp;
j++;
}
greatest=c[0];
for(i=0;i<10;i++)
if(c[i]>greatest){
greatest=c[i];
}
}
Cout<<”Greatest of ten numbers is %d”, greatest);
return 0;
}

OUTPUT

10987654321
EX.NO: 13
CLASS TEMPLATE
PROGRAM
#include<iostream.h>
#include<conio.h>
template<class T, class U>
class A {
T x;
U y;
public:
A() { cout<<"Constructor Called"<<endl; }
};

int main() {
clrscr();
cout<<"\n*********************";
cout<<"\n\tCLASS TEMPLATE";
cout<<"\n\t********************";
A<char, char> a;
A<int, double> b;
return 0;
}

OUTPUT
EX.NO: 14
FUNCTION TEMPLATE

PROGRAM

#include <iostream.h>
template<class T> T add(T &a,T &b)
{
T result = a+b;
return result;

}
int main()
{
int i =2;
int j =3;
float m = 2.3;
float n = 1.2;
clrscr();
cout<<"\n************************";
cout<<"\n\tFUNCTION TEMPLATE";
cout<<"\n\t***********************";
cout<<"Addition of i and j is :"<<add(i,j);
cout<<'\n';
cout<<"Addition of m and n is :"<<add(m,n);
return 0;
}

OUTPUT
EX.NO: 15
EXCEPTION HANDLING
PROGRAM:

#include <iostream>
using namespace std;
float division(int x, int y) {
if( y == 0 ) {
throw "Attempted to divide by zero!"; }
return (x/y); }
int main () {
int i = 25;
int j = 0;
float k = 0;
try {
k = division(i, j);
cout << k << endl;
}catch (const char* e) {
cerr << e << endl; }
return 0; }

OUTPUT
Attempted to divide by zero!

You might also like