0% found this document useful (0 votes)
26 views49 pages

C++ Final Record

Uploaded by

sgprakash447
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views49 pages

C++ Final Record

Uploaded by

sgprakash447
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

Department of Computer Science

Name :

Reg. No. :

Subject :

Year :

KALAIGNAR KARUNANIDHI GOVERNMENT ARTS COLLEGE


TIRUVANNAMALAI – 606 603
KALAIGNAR KARUNANIDHI GOVERNMENT ARTS
COLLEGE
TIRUVANNAMALAI – 606 603

Department of Computer Science

PRACTICAL RECORD

NAME : Year :

ROLL No. : Reg. No :

BATCH : Time :
THIRUVALLUVAR UNIVERSITY
KALAIGNAR KARUNANIDHI GOVERNMENT ARTS COLLEGE
THIRUVANNAMALAI – 606 603
Department of Computer Science

PRACTICAL RECORD

Certified bonafied record of practical work done by

with
Reg. No.---------------------------------- in this department during the
academic year 20 – 20 .

Lecturer Incharge Head of the Department


Date : Computer Science
KK Government Arts College
Tiruvannamalai.

Submitted for the Degree University


Practical Examination held on

Reg. No.

EXAMINER
1. 2.
INDEX

S.NO. Date CONTENT PAGE SIGN


NO.

1 Class and Objects 1

2 Constructor, copy constructor and Destructor. 5

3 Function overloading, Default Arguments and Inline 9


function.

4 Friend Functions. 13

5 Passing Objects to Functions

6 Pointers and dynamic memory allocation using new and d 17


delete Operators

7 Unary Operator Overloading 21

8 Binary Operator Overloading 25

9 Inheritance:

• Single Inheritance 29

• Multilevel Inheritance 33

• Multiple Inheritance 37

• Hierarchical Inheritance 40

10 Virtual Functions. 44

11 Manipulate a Text File. 48

12 Sequential I/O Operations on a file. 52

13 Command Line Arguments 56

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:

Enter the value for a&b 7 8

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:

1001 Abitha 10000


Copy Constructor Called
1001 Abitha 10000
3.1. FUNCTION OVERLOADING
PROGRAM:
#include<iostream.h>
#include<conio.h>
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14*r*r*h);
}
long volume(long l,intb,int h)
{
return(l*b*h);
}
void main()
{
clrscr();
cout<<"!!!VOLUME!!!\n;
cout<<volume(10)<<endl;
cout<<volume(10,20)<<endl;
cout<<volume(10,20,30)<<endl;
getch();
}
OUTPUT:

1000
6280
6000
3.2 DEFAULT ARGUMENTS
#include <iostream>
using namespace std;

int sum(int x, int y, int z = 0, int w = 0) //assigning default values to z,w as 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
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;
}

inline void operation :: sum()


{
add = n1+n2;
cout<< "Addition of two numbers: " << n1+n2 << "\n";
}
int main()
{
cout<< "Program using inline function\n";
operation s;
s.TakeValues();
s.sum();
return 0;
}
OUTPUT:

Enter first value: 3


Enter second value: 5
Addition of two numbers: 8
4. FRIEND FUNCTIONS
PROGRAM:
#include<iostream.h>
#include<conio.h>
class sum {
int a,b,c;
public: void input()
{
cout<<"Enter the value for a:"; cin>>a;
cout<<"Enter the value for b:"; cin>>b;
}
void add()
{ c=a+b; }
void display()
{ cout<<"The result is ="<<c<<endl; }
};
void max() {
int a,b;
cout<<"Enter the value for a :"; cin>>a;
cout<<"Enter the value for b :"; cin>>b;
if(a>b)
{ cout<<"The Greatest Value is"<<a<<endl; }
else
{cout<<"The Greatest value = "<<b<<endl; }
void main()
{
clrscr();
sum t;
t.input();
t.add();
t.display();
max();
getch();
}
OUTPUT:

Enter the value for a: 4


Enter the value for b: 6
The result is =10
Enter the value for a : 4
Enter the value for b : 6
The Greatest value = 6
5. PASSING OBJECTS TO FUNCTION
PROGRAM:
#include<iostream.h>
#include<conio.h>
class weight {
int kg, grm;
public:
void getdata();
void showdata();
void adddata(weight w1,weight w2);
};
void weight::getdata()
{
cout<<"Enter kg and grm"<<endl; cin>>kg>>grm;
}
void weight::showdata()
{
cout<<"\nkg is"<<kg; cout<<"\ngrm is"<<grm;
}
void weight::adddata(weight w1,weight w2)
{
grm=w1.grm+w2.grm;
kg=grm/1000; grm=grm%1000; kg=kg+w1.kg+w2.kg;
}
void main()
{
clrscr();
weight w1,w2,w3;
w1.getdata();
w2.getdata();
w3.adddata(w1,w2);
w1.showdata();
w2.showdata();
w3.showdata();
getch();
}
OUTPUT:

Enter kg and grm


3
500
Enter kg and grm
7 400
kg is 3
grm is 500 kg is 7
grm is 400 kg is 10 grm is 900
6. POINTERS AND DYNAMIC MEMORY ALLOCATION
#include <iostream>
using namespace std;
int main()
{
int* p = NULL;
p = new (nothrow) int;
if (!p)
cout << "allocation of memory failed\n";
else {
*p = 29;
cout << "Value of p: " << *p << endl;
}
float* r = new float(75.25);
cout << "Value of r: " << *r << endl;
int n = 5;
int* q = new (nothrow) int[n];
if (!q)
cout << "allocation of memory failed\n";
else {
for (int i = 0; i < n; i++)
q[i] = i + 1;
cout << "Value store in block of memory: ";
for (int i = 0; i < n; i++)
cout << q[i] << " ";
}
delete p;
delete r;
delete[] q;
return 0;
}
OUTPUT:

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:

Enter the real: 23


Enter the imaginary: 45
Enter the real: 33
Enter the imaginary: 57
The result is 56 +i102
9.1. SINGLE INHERITANCE
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base {
private: int a,b;
public:
void input()
{ cout<<"\n\n\n\tEnter the value :"; cin>>a>>b;}
void show()
{ cout<<"a:"<<a<<"\nb:"<<b<<endl; }
};
class derive:public base
{
private: int m,n;
public:
void getdata()
{ cout<<"\n Enter the value:"; cin>>m>>n; }
void display()
{ cout<<"m="<<m<<"\nn="<<n<<endl; }
};
void main() {
clrscr();
cout<<"\t\t\t\t SINGLE INHERITANCE" ;
derive obj1;
cout<<"\n\t\t BASE CLASS";
obj1.input();
obj1.show();
cout<<"\n\t\t DERIVED CLASS";
obj1.getdata();
obj1.display();
getch();
}
OUTPUT:

Enter the value: 2


3
a:2,b:3
Enter the value: 4
5
M=4 N=5
9.2 MULTI-LEVEL INHERITANCE
PROGRAM:
#include<iostream>
class A
{ protected:
char name[20]; int age;
};
class B:public A
{ protected:
int w; float h;
};
class C:public B
{ public:
char g;
void get_data()
{
cout<<“Enter name and age:\n”; cin>>name>>age;
cout<<“Enter weight and height:\n”; cin>>w>>h;
cout<<“Enter gender:”; cin>>g;
}
void show()
{
cout<<“\nName: “<<name<<endl<<“Age: “<<age<<endl;
cout<<“Weight: “<<w<<endl<<“Height: “<<h<<endl;
cout<<“Gender: “<<g<<endl;
}
};
int main()
{
C ob;
ob.get_data();
ob.show();
}
OUTPUT:

Enter name and age:


Madhu 35
Enter weight and height:
58 5.6
Enter gender: F
Name: Madhu
Age: 35
Weight: 58
Height: 5.6
Gender: F
9.3. MULTIPLE INHERITANCE
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student {
protected: int rollno,m1,m2;
public:
void get() {
cout<<"\nEnter the roll number:"; cin>>rollno;
cout<<"Enter the mark1:"; cin>>m1;
cout<<"Enter the mark2:"; cin>>m2; }
};
class sports {
protected:
int sm;
public:
void getsm()
{ cout<<"Enter the sports mark:";cin>>sm; }
};
class report:public student,public sports {
int tot,avg;
public:
void display() {
tot=m1+m2+sm; avg=tot/3;
cout<<"\nTotal:"<<tot;
cout<<"\nAverage:"<<avg; }
};
void main() {
clrscr();
report obj;
cout<<"Student Details";
obj.get();
obj.getsm();
obj.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:

Enter name: Ramu


Enter weight and height: 63 5.8
Enter age: 26
Enter gender: M
This is class B and it is inherited form class A
Name: Ramu
Weight: 63
Height: 5.8
This is class C and it is inherited form class A
Age: 26
Gender: M
10. VIRTUAL FUNCTION
PROGRAM:
#include<iostream.h>
#include<conio.h>
class base {
public:
virtual void display()
{ cout<<"Base class display is called\n"; }
};
class sub1:public base {
public:
void display()
{ cout<<"\n Derived class 1 display is called\n"; }
};
class sub2:public base {
public:
void display()
{ cout<<"\n Derived class 2 display is called\n"; }
};
void main() {
clrscr();
base*bptr,b;
sub1 d1; sub2 d2;
cout<<"\t VIRTUAL FUNCTION\n";
cout<<"\t------------------\n";
bptr=&b;
bptr->display();
bptr=&d1;
bptr->display();
bptr=&d2;
bptr->display();
getch();
}
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:

ASCII file processing


int value=12345;float value=6789.12
binary file processing
int value=12345;float value=6789.12
12. SEQUENTIAL I/O OPERATION FILE
PROGRAM:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
int main()
{
clrscr();
char string[80];
cout<<"Enter a string\n";
cin>>string;
int len=strlen(string);
fstream file;
file.open("TEXT",ios::in|ios::out);
for(int i=0;i<len;i++)
file.put(string[i]);
file.seekg(0);
char ch;
while(file)
{
file.get(ch);
cout<<ch;
}
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:

Addition of member1 and member2:30


Addition of member1 and member2:30.46
15. FUNCTION TEMPLATE
PROGRAM:
#include<iostream.h>
template<class T>
T large(T n1,T n2)
{
return(n1>n2)?n1:n2;
}
int main()
{
int i1,i2;
float f1,f2;
char c1,c2;
cout<<"Enter two integers:\n";
cin>>i1>>i2;
cout<<Large(i1,i2)<<"is larger:"<<endl;
cout<<"\nEnter two floating-point numbers:\n";
cin>>f1>>f2;
cout<<Large(f1,f2)<<"is larger:"<<endl;
cout<<"\nEnter two characters:\n";
cin>>c1>>c2; cout<<Large(c1,c2)<<"has
larger ASCII value:"; return 0;
}
OUTPUT:

Enter two integers:


5
10
10 is larger.
Enter two floating-point numbers:
12.4
10.2
12.4 is larger
Enter two characters: Z
Z
Z has larger ASCII value.
16. EXCEPTION HANDLING
PROGRAM:
#include<iostream.h>
using namespace std;
double divide (int x, int y)
{
if(y==0)
{
throw “Divide by zero condition occurred”;
}
return (x/y);
}
int main()
{
int a=100;
int b=0;
int c;
try
{
c=divide(a,b);
cout<<”The value of c is :”<<c<<endl;
}
catch(char const * errmsg)
{
cout<<errmsg;
}
return 0;
}
OUTPUT:

Divide by zero condition occurred

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy