C++ Final Record
C++ Final Record
Name :
Reg. No. :
Subject :
Year :
PRACTICAL RECORD
NAME : Year :
BATCH : Time :
THIRUVALLUVAR UNIVERSITY
KALAIGNAR KARUNANIDHI GOVERNMENT ARTS COLLEGE
THIRUVANNAMALAI – 606 603
Department of Computer Science
PRACTICAL RECORD
with
Reg. No.---------------------------------- in this department during the
academic year 20 – 20 .
Reg. No.
EXAMINER
1. 2.
INDEX
4 Friend Functions. 13
9 Inheritance:
• Single Inheritance 29
• Multilevel Inheritance 33
• Multiple Inheritance 37
• Hierarchical Inheritance 40
10 Virtual Functions. 44
14 Class Template 60
15 Function Template. 64
16 Exception Handling. 66
1. CLASS AND OBJECT
PROGRAM:
#include<iostream.h>
#include<conio.h>
class sum
{
int a,b,c;
public:
void input()
{
cout<<"Enter the value for a&b";
cin>>a>>b;
}
void add()
{
c=a+b;
}
void display()
{
cout<<"The result is "<<c;
}
};
void main()
{
clrscr();
sum s;
s.input();
s.add();
s.display()
;
getch();
}
OUTPUT:
The result is 15
2.1 CONSTRUCTOR AND DESTRUCTOR
PROGRAM:
#include<iostream>
class dist
{
public:
int feet,inch;
dist(){ }
dist (int x,int y)
{ feet=x; inch =y; }
dist (float m,float n)
{ feet=m; inch=n; }
void display()
{
cout<<“Resulted distance is: “;
cout<<feet<<” feet “<<inch<<” inch\n”;
}
void sum(dist obj1,dist obj2)
{
feet=obj1.feet+obj2.feet; inch=obj1.inch+obj2.inch;
if(inch>=12)
{ feet=feet+1; inch=inch-12; }
}
~dist()
{cout<<“object deleted:\n”; }
};
int main()
{
dist obj1(8,9),obj2(4.0f,7.3f),obj3;
obj3.sum(obj1,obj2);
obj3.display();
}
OUTPUT:
Object deleted.
Object deleted.
Resulted distance is: 13 feet 4 inch
Object deleted.
Object deleted.
Object deleted.
2.2 COPY CONSTRUCTOR
#include <iostream>
#include <string.h>
using namespace std;
class student
{
int rno;
string name;
double fee;
public:
student(int, string, double);
student(student& t)
{
rno = t.rno; name = t.name; fee = t.fee;
cout << "Copy Constructor Called" << endl;
}
void display();
};
student::student(int no, string n, double f)
{
rno = no; name = n; fee = f;
}
void student::display()
{
cout << rno << "\t" << name << "\t" << fee << endl;
}
int main()
{
student s(1001, "Abitha", 10000);
s.display();
student manjeet(s);
manjeet.display();
return 0;
}
OUTPUT:
1000
6280
6000
3.2 DEFAULT ARGUMENTS
#include <iostream>
using namespace std;
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
3.3 INLINE FUNCTION
PROGRAM:
#include <iostream>
using namespace std;
class operation
{
int n1,n2,add;
float div;
public:
void TakeValues();
void sum();
};
inline void operation :: TakeValues()
{
cout<< "Enter first value:";
cin>> n1;
cout<< "Enter second value:";
cin>> n2;
}
Value of p: 29
Value of r: 75.25
Value store in block of memory: 1 2 3 4 5
7. UNARY OPERATOR OVERLOADING
PROGRAM:
#include<iostream.h>
#include<conio.h>
class space
{
int x,y,z;
public:
void getdata(int a,intb,int c);
void display(void);
void operator-();
};
void space::getdata(int a,intb,int c)
{
x=a; y=b; z=c;
}
void space::display(void)
{ cout<<x<<" "; cout<<y<<" "; cout<<z<<"\n ";
}
void space::operator-()
{ x=-x; y=-y; z=-z; }
void main()
{
clrscr();
space s;
s.getdata(10,-20,30);
cout<<"s:";
s.display();
-s;
cout<<"s:";
s.display();
getch();
}
OUTPUT:
S: 10 -20 30
S: -10 20 -30
8. BINARY OPERATOR OVERLOADING
PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
int real,img;
public:
void input()
{
cout<<”Enter the real:”; cin>>real;
cout<<”Enter the imaginary:”; cin>>img;
}
complex operator+(complex c)
{
complex t;
t.real=real+c.real;
t.img=img+c.img;
return t;
}
void display()
{ cout<<”The result is”<<real<<”+i”<<img; }
};
void main()
{
Clrscr();
Complex s1,s2,s3;
s1.input();
s2.input();
s3=s1+s2;
s3.display();
getch();
}
OUTPUT:
STUDENT DETAILS
Enter the Roll Number: 123456
Enter the mark 1:90
Enter the mark 2:80
Enter the sports mark: 95
Total: 265
Avg: 88
9.4 HIERARCHICAL INHERITANCE
PROGRAM:
#include<iostream>
using namespace std;
class A
{
protected:
char name[20];
int age;
};
class B:public A
{
public:
float h;
int w;
void get_data1()
{
cout<<“Enter name:”;
cin>>name;
cout<<“Enter weight and height:”;
cin>>w>>h;
}
void show()
{
cout<<“This is class B and it is inherited from Class A\n”;
cout<<“Name: “<<name<<endl;
cout<<“Weight: “<<w<<endl;
cout<<“Height: “<<h<<endl;
}
};
class C:public A
{
public:
char gender;
void get_data2()
{
cout<<“Enter age:”;
cin>>age;
cout<<“Enter gender:”;
cin>>gender;
}
void show()
{
cout<<“This is class C and it is inherited from class A\n”;
cout<<“Age: “<<age<<endl;
cout<<“Gender: “<<gender<<endl;
}
};
int main()
{
B ob;
C ob1;
ob.get_data1();
ob1.get_data2();
ob.show();
ob1.show();
}
OUTPUT:
VIRTUAL FUNCTION
Base class display is called
Derived class 1 display is called
Derived class 2 display is called
11. MANIPULATE A TEXT FILE
PROGRAM:
#include<iostream.h>
#include<fstream.h>
int main()
{
int var1=12345; float var2=6789.123;
cout<<”ASCII file processing”<<endl;
ofstream out;
out.open(“textintfloat.txt”,ios::out);
out<<var1<<””<<var2<<endl;
out.close();
int var3; float var4;
ifstream in;
in.open(“textintfloat.txt”,ios::in);
in>>var3>>var4;
in.close();
cout<<”int value=”<<var3<<;float value=”<<var4<<endl;
cout<<”binary file processing”<<endl;
ofstream outbin;
outbin.open(“binintfloat.bin”,ios::out|ios::binary);
outbin.write((char *)&var1,sizeof(var1));
outbin.write((char *)&var2,sizeof(var2));
outbin.close();
var3=0; var4=0;
ifstream inbin;
inbin.open(“binintfloat.bin”, ios::out|ios::binary);
inbin.read((char *)&var3,sizeof(var3));
inbin.read((char *)&var4,sizeof(var4));
cout<<”int value=”<<var3<<;float value=”<<var4<<endl;
inbin.close();
return 0;
}
OUTPUT:
Enter a string
Cobol_progamming input
Cobol_programming output
13. COMMAND LINE ARGUMENTS
PROGRAM:
#include<iostream.h>
#include<conio.h>
void main(int argc, char* argv[])
{
int i;
clrscr();
cout<<”Total number of arguments:”<<argc;
for(i=0;i<argc;i++)
{
cout<<endl<<i;<<”argument:”<<argv[i];
getch()
}
}
OUTPUT:
C:/TC/BIN>TCC mycmd.cpp
C:/TC/BIN>mycmd 10 20
Number of Arguments: 3
0 arguments c:/tc/bin/mycmd.exe
1 arguments: 10
2 arguments: 20
14. CLASS TEMPLATE
PROGRAM:
#include<iostream.h>
using namespace std;
template<class>
class Myclass
{
private:
X member1;
X member2;
public:
void add_values()
{
cout<<”Addition of member1 and member2:”<<member1+member2<<endl;
}
Myclass(X a,Xb)
{
member1=a;
member2=b;
}
};
int main()
{
Myclass<int>object1(10,20);
object1.add_values();
Myclass<float>object2(10.12,20.34)
object2.add_values();
return 0;
}
OUTPUT: