C++ Lab Manual
C++ Lab Manual
Vision
To achieve the autonomous & university status and spread universal education by
inculcating discipline, character and knowledge into the young minds and mould
them into enlightened citizens.
Mission
Our mission is to impart education, in a conducive ambience, as comprehensive as
possible, with the support of all the modern technologies and make the students
acquire the ability and passion to work wisely, creatively and effectively for the
betterment of our society.
Vision
Serving the high quality educational needs of local and rural students within the
core areas of Computer Science and Engineering and Information Technology
through a rigorous curriculum of theory, research and collaboration with other
disciplines that is distinguished by its impact on academia, industry and society.
PEO-1: The graduates of Computer Science and Engineering will have successful career in technology or
managerial functions.
PEO-2: The graduates of the program will have solid technical and professional foundation to continue higher
studies.
PEO-3: The graduates of the program will have skills to develop products, offer services and create new knowledge.
PEO-4: The graduates of the program will have fundamental awareness of Industry processes, tools and
technologies.
Program Outcomes (PO’s):
F. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent
responsibilities relevant to the Computer Science and Engineering professional
engineering practice.
G. Environment and sustainability: Understand the impact of the Computer Science and
Engineering professional engineering solutions in societal and environmental contexts,
and demonstrate the knowledge of, and need for sustainable development.
H. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
I. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
J. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
K. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
L. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change.
2. LAB OUTCOME
Upon successful completion of this Lab the student will be able to:
1. Write a C++ Program to display Names, Roll No., and grades of 3 students who have appeared in
the examination. Declare the class of name, Roll No. and grade. Create an array of class objects.
Read and display the contents of the array.
2. Write a C++ program to declare Struct. Initialize and display contents of member variables.
3. Write a C++ program to declare a class. Declare pointer to class. Initialize and display the contents
of the class member.
4. Given that an EMPLOYEE class contains following members: data members: Employee number,
Employee name, Basic, DA, IT, Net Salary and print data members.
5. Write a C++ program to read the data of N employee and compute Net salary of each employee
(DA=52% of Basic and Income Tax (IT) =30% of the gross salary).
7. Write a C++ program to use scope resolution operator. Display the various values of the same
variables declared at different scope levels.
9. Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)
10. Write a C++ program to create an array of pointers. Invoke functions using array objects.
11. Write a C++ program to use pointer for both base and derived classes and call the member
function. Use Virtual keyword.
Reference Books :
1. The C++ Programming Language, 3rd Edition, B. Stroutstrup, Pearson Education.
3. Object Oriented Programming in C++, 3rd Edition, R. Lafore, Galigotia Publications Pvt Ltd.
2 Write a C++ program to declare Struct. Initialize and display contents of member
12
variables.
3 Write a C++ program to declare a class. Declare pointer to class. Initialize and display
13
the contents of the class member.
4 Given that an EMPLOYEE class contains following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary and print data members. 14-15
5 Write a C++ program to read the data of N employee and compute Net salary of each
employee (DA=52% of Basic and Income Tax (IT) =30% of the gross salary). 16-17
7 Write a C++ program to use scope resolution operator. Display the various values of the 19
same
8 variables declared
Write a C++ at different
program scope
to allocate levels.using new operator.
memory 20
9 Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3) 21-22
10 Write a C++ program to create an array of pointers. Invoke functions using array objects. 23-24
11 Write a C++ program to use pointer for both base and derived classes and call the member 25
function. Use Virtual keyword.
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
void getDetails(void); //member function to get student's details
void putDetails(void); //member function to print student's details
};
int main()
{
student std[MAX]; //array of objects creation
int n,loop;
cout << "Enter total number of students: ";
cin >> n;
for (loop=0;loop< n; loop++)
{
cout << "Enter details of student " << loop+1 << ":\n";
std[loop].getDetails();
}
cout << endl;
for(loop=0;loop< n; loop++)
{
cout << "Details of student " << (loop+1) << ":\n";
std[loop].putDetails();
}
return 0;
}
Output
Enter total number of students: 3
Enter details of student 1:
Enter name: Karthik
Enter roll number: 1201
Enter total marks out of 500: 456
Details of student 1:
Student details:
Name: Karthik, Roll Number: 101, Total: 456, Percentage: 91.2
Details of student 2:
Details of student 3:
Student details:
Name: Kiran, Roll Number: 1203, Total: 398, Percentage:79.6
2. Write a C++ program to declare Struct. Initialize and display contents of member variables.
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
student s;
cout << "Enter information," << endl;
cout << "Enter name: ";
cin >> s.name;
cout << "Enter roll number: ";
cin >> s.roll;
cout << "Enter marks: ";
cin >> s.marks;
cout << "\nDisplaying Information," << endl;
cout << "Name: " << s.name << endl;
cout << "Roll: " << s.roll << endl;
cout << "Marks: " << s.marks << endl;
return 0;
}
Output:
Enter information,
Enter name: Bill
Enter roll number: 4
Enter marks: 55.6
3. Write a C++ program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member.
#include <iostream>
using namespace std;
class Box
{
public:
Box(double l = 2.0, double b = 2.0, double h = 2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
private:
double length;
double breadth;
double height;
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5);
Box Box2(8.5, 6.0, 2.0);
Box *ptrBox;
ptrBox = &Box1;
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
ptrBox = &Box2;
cout << "Volume of Box2: " << ptrBox->Volume() << endl;
return 0;
}
Output:
Details of : Madhav
Employee number: 5123
Basic salary : 10000
Employee DA : 5200
Income Tax : 4560
Net Salary : 10640
#include<iostream.h>
#include<conio.h>
#define SIZE 5
class emp
{
float basic,da,it,netsal;
char name[20],num[10];
public:
void getdata();
void net_sal();
void dispdata();
};
void emp::getdata()
{
cout<<"\n Enter employee number: " ;
cin>>name;
cout<<"\n Enter employee name: " ;
cin>>num;
cout<<"Enter employee basic salary in Rs: " ;
cin>>basic;
}
void emp::net_sal()
{
da=((0.52)*basic );
float gsal=da+basic;
it=((0.3)*gsal);
netsal=gsal-it;
}
void emp::dispdata()
{
cout <<"\n Employee number: "<<name
cout <<"\n Employee name: "<<num
cout <<"\n Employee netsalary: "<<netsal<<" Rs.";
}
void main()
{
clrscr();
Output:
***********************************"
Calculation of Employee Net Salary
***********************************"
Enter the number of employees: 1
-----------------
Employee Detail::
-----------------
Employee:1
Employee number: 22
Employee name: Sanath
Employee netsalary: 10000 RS.
int main()
{
string filename = "test.txt";
ofstream fout(filename.c_str()); // default mode is ios::out | ios::trunc
if (!fout)
{
cerr << "error: open file for output failed!" << endl;
abort(); // in <cstdlib> header
}
if (!fin)
{
cerr << "error: open file for input failed!" << endl;
abort();
}
char ch;
while (fin.get(ch))
{ // till end-of-file
cout << ch;
}
7. Write a C++ program to use scope resolution operator. Display the various values of the
same variables declared at different scope levels.
#include <iostream>
using namespace std;
class programming
{
public: void output(); //function declaration
};
void programming::output()
{
cout << "Function defined outside the class.\n";
}
int main()
{
programming x;
x.output();
return 0;
}
Ouput:
#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;
Output:
Value of p: 29
Value of r: 75.25
Value store in block of memory: 1 2 3 4 5
9. Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)
#include <iostream>
using namespace std;
int main()
{
derive2 a;
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
}
Output:
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18
#include<iostream.h>
#include<constream.h>
class A
{
public:
virtual void show()
{
cout<<“A\n”;
}
};
class B : public A
{
public:
void show()
{
cout<<“B\n”;
}
};
class C : public A
{
public:
void show()
{
cout<<“C\n”;
}
};
class E : public A
{
public:
void show()
{
cout<<“E”;
}
};
void main()
{
clrscr();
A a;
B b;
C c;
D d;
E e;
A *p a[]={&a,&b,&c,&d,&e};
for ( int j=0;j<5;j++)
pa[j]->show();
}
Output:
A
B
C
D
E
void show ()
{
cout<< "show derived class" <<endl;
}
};
int main()
{
Output:
print derived class
show base class