0% found this document useful (0 votes)
101 views

C++ Lab

The document describes a C++ programming lab manual. It lists 11 lab experiments covering topics like declaring classes and structs, inheritance, pointers, arrays, and virtual functions. The objectives are to introduce object-oriented programming concepts in C++, including abstraction, inheritance, polymorphism, and exception handling. Students will learn to develop applications using these OOP techniques.

Uploaded by

chintu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

C++ Lab

The document describes a C++ programming lab manual. It lists 11 lab experiments covering topics like declaring classes and structs, inheritance, pointers, arrays, and virtual functions. The objectives are to introduce object-oriented programming concepts in C++, including abstraction, inheritance, polymorphism, and exception handling. Students will learn to develop applications using these OOP techniques.

Uploaded by

chintu
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 20

C++ PROGRAMMING LAB

OBJECT ORIENTED PROGRAMMING USING C++ LAB

LAB MANUAL

B.TECH II YEAR I SEM (R18)

( 2021-22 )

COMMON TO ( CSE, IT)

MALLA REDDY ENGINEERING COLLEGE AND


MANAGEMENT SCIENCES
(Approved by AICTE, New Delhi and Affiliated to JNTUH, Hyderabad)
Kistapur (v),Medchal (M),Medchal- 501401

MREM (CSE, IT) Page 1


C++ PROGRAMMING LAB

Prerequisites : A course on “ Programming for Problem Solving ”.

Course Objectives:

 Introduces object-oriented programming concepts using the C++ language.


 Introduces the principles of data abstraction, inheritance and polymorphism;
 Introduces the principles of virtual functions and polymorphism
 Introduces handling formatted I/O and unformatted I/O
 Introduces exception handling

Course Outcomes:
 Ability to develop applications for a range of problems using object-oriented
programming
Techniques

MREM (CSE, IT) Page 2


C++ PROGRAMMING LAB

List of Lab Experiments


S.NO. LIST OF LAB EXPERIMENTS PAGE
NO.
1 Write a C++ Program to display Names, Roll No., and grades of 3 students who have ap-
peared 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).

6 Write a C++ to illustrate the concepts of console I/O operations.

7 Write a C++ program to use scope resolution operator. Display the various values of the
same
8 Write a C++ program to allocate memory using new operator.

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.

MREM (CSE, IT) Page 3


C++ PROGRAMMING LAB

1. write a c++ program to display names, roll no, grades of 3 students who have appeared
in the examination
Aim: To display names, roll no, grades of 3 students who have appeared in the examination

Program Code:
#include <iostream.h>
#include <conio.h>
struct student
{
char name[50];
int roll;
float marks;
char grade;
}
s[10];
int main()
{
clrscr();
cout << "Enter information of students: " << endl;
// storing information
for(int i = 0; i < 3; ++i)
{
s[i].roll = i+1;
cout << "For roll number" << s[i].roll << "," << endl;
cout << "Enter name: ";
cin >> s[i].name;
cout << "Enter marks: ";
cin >> s[i].marks;
cout << "Enter grade: ";
cin >> s[i].grade;
cout << endl;
}
cout << "Displaying Information: " << endl;
// Displaying information
for(i = 0; i < 3; ++i)
{
cout << "\nRoll number: " << i+1 << endl;
cout << "Name: " << s[i].name << endl;
cout << "Marks: " << s[i].marks << endl;
cout << "Grade: " << s[i].grade << endl;
}
getch();
return 0;
MREM (CSE, IT) Page 4
C++ PROGRAMMING LAB

Output :
Enter information of students:
For roll number 1
Enter name: Murali
Enter marks: 90
Enter grade: A
For roll number 2
Enter name: Madhu
Enter marks: 60
Enter grade: B
For roll number 3
Enter name: Navi
Enter marks: 40
Enter grade: C
Displaying Information:
Roll number: 1
Name: Murali
Marks:90
Grade: A
Roll number: 2
Name: Madhu
Marks:60
Grade: B
Roll number: 3
Name: Navi
Marks:40
Grade: a

MREM (CSE, IT) Page 5


C++ PROGRAMMING LAB

2.write a C++ program to declare Struct initialize and display contents of member
variables
Aim: To declare Struct, initialize and display contents of member variables
Program Code :

#include<iostream.h>
#include<conio.h>
struct student
{
char name[50];
int roll;
float marks;
};

int main()
{
student s;
clrscr();
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;
getch();
return 0;
}

Output :
Enter information,
Enter name: Bill
Enter roll number: 4
Enter marks: 55.6
Displaying Information,
Name: Bill
Roll: 4 Marks: 55.6

MREM (CSE, IT) Page 6


C++ PROGRAMMING LAB

3.write a c++ program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member.
Aim: program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member.

Program Code :
#include<iostream.h>
class Box
{
public:
// Constructor definition
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; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};

int main(void) {
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
Box *ptrBox; // Declare pointer to a class.

// Save the address of first object


ptrBox = &Box1;

// Now try to access a member using member access operator


cout << "Volume of Box1: " << ptrBox->Volume() << endl;

// Save the address of second object


ptrBox = &Box2;

// Now try to access a member using member access operator


cout << "Volume of Box2: " << ptrBox->Volume() << endl;

MREM (CSE, IT) Page 7


C++ PROGRAMMING LAB

return 0;
}

Out put:
Constructor called.
Constructor called.
Volume of Box1: 5.94
Volume of Box2: 10.67

4. Give that an employee class contains following members:


Employee number, Employee name, Basic, DA , IT Net Salary and print data members.

Aim : implementing the employee class contains following members:


Employee number, Employee name, Basic, DA , IT Net Salary and print data
members

Program Code :

#include<iostream.h>
#include<conio.h>
class employee
{
int emp_num;
char emp_name[20];
float emp_basic;
float sal;
float emp_da;
float net_sal;
float emp_it;
public:
void get_details();
void show_emp_details();
};
void employee :: get_details()
{
cout<<"\nEnter employee number:\n";
cin>>emp_num;
cout<<"\nEnter employee name:\n";
cin>>emp_name;
cout<<"\nEnter employee basic:\n";
cin>>emp_basic;
cout<<"\nEnter employee sal:\n";

MREM (CSE, IT) Page 8


C++ PROGRAMMING LAB

cin>>sal;
cout<<"\nEnter employee da:\n";
cin>>emp_da;
cout<<"\nEnter employee net sal:\n";
cin>>net_sal;
cout<<"\nEnter employee it:\n";
cin>>emp_it;
}
void employee :: show_emp_details()
{
cout<<"\n\n\nDetails of : "<<emp_name;
cout<<"\n\nEmployee number: "<<emp_num;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nSalary : "<<sal;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax : "<<emp_it;
cout<<"\nNet Salary : "<<net_sal;
}
int main()
{
employee emp[10];
int i,num;
clrscr();
cout<<"\nEnter number of employee details\n";
cin>>num;
for(i=0;i<num;i++)
emp[i].get_details();
for(i=0;i<num;i++)
emp[i].show_emp_details();
getch();
return 0;
};

Sample Output:
Enter number of employee details 1
Enter employee number: 101
Enter employee name: Raghu
Enter employee basic: 16000
Enter employee sal: 30000
Enter employee da: 800
Enter employee net sal: 25000
Enter employee it: 1000
Details of : Raghu
Employee number: 101

MREM (CSE, IT) Page 9


C++ PROGRAMMING LAB

Basic salary : 16000


Salary : 30000
Employee DA : 800
Income Tax : 1000
Net Salary : 250000

5. Write a C++ program to read data on N employees and compute the Net_Sal of each
employee ( DA = 52% of Basic and Income Tax = 30% of the gross salary ).
Aim: implement a C++ program to read data on N employees and compute the Net_Sal of each
employee ( DA = 52% of Basic and Income Tax = 30% of the gross salary ).
Program code:
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_num;
char emp_name[20];
float emp_basic;
float sal;
float emp_da;
float net_sal;
float emp_it;
public:
void get_details();
void find_net_sal();
void show_emp_details();
};
void employee :: get_details()
{
cout<<"\nEnter employee number:\n";
cin>>emp_num;
cout<<"\nEnter employee name:\n";
cin>>emp_name;
cout<<"\nEnter employee basic:\n";
cin>>emp_basic;
}
void employee :: find_net_sal()
{
emp_da=0.52*emp_basic;
emp_it=0.30*(emp_basic+emp_da);
net_sal=(emp_basic+emp_da)-emp_it;
}

MREM (CSE, IT) Page 10


C++ PROGRAMMING LAB

void employee :: show_emp_details()


{
cout<<"\n\n\nDetails of : "<<emp_name;
cout<<"\n\nEmployee number: "<<emp_num;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax : "<<emp_it;
cout<<"\nNet Salary : "<<net_sal;
}
int main()
{
employee emp[10];
int i,num;
clrscr();
cout<<"\nEnter number of employee details\n";
cin>>num;
for(i=0;i<num;i++)
emp[i].get_details();
for(i=0;i<num;i++)
emp[i].find_net_sal();

for(i=0;i<num;i++)
emp[i].show_emp_details();
getch();
return 0;

out put :

MREM (CSE, IT) Page 11


C++ PROGRAMMING LAB

6.write a c++ program to illustrate the concepts of console I/O operations


Aim : implement a c++ program to illustrate the concepts of console I/O operations

Program Code:
#include<iostream.h>
int main( ) {
char sample[] = "Murali Mohan Reddy";
int age;
cout << "Enter your age:";
cin >> age;
cout << sample << " - A computer science professor for c++";
cout << "\nYour age is: "<<age;
cerr << "An error occured";
return 0;
}

Out Put:

Enter your age: 38


Murali Mohan Reddy- A computer science professor for c++
Your age is: 38
An error occured

MREM (CSE, IT) Page 12


C++ PROGRAMMING LAB

7. write a c++ program to use scope resolution operator. Display the various values of the
same variables declared at different scope levels.

Aim : To Display the various values of the same variables declared at different scope
levels.
Program output:
#include<iostream>
int n = 12; //global variable
int main()
{
int n = 13; //local variable
cout << ::n << endl; //print global variable:12
cout << n << endl; //print the local variable:13
}

Output :
12
13

MREM (CSE, IT) Page 13


C++ PROGRAMMING LAB

8. write a c++ program to allocate memory using new operator

Aim: implement a c++ program to allocate memory using new operator

Program code:

#include <iostream>
using namespace std;
class Box {
public:
Box()
{
cout << "Constructor called!" <<endl;
}
~Box()
{
cout << "Destructor called!" <<endl;
}
};

int main()
{
Box* myBoxArray = new Box[4];
delete [] myBoxArray; // Delete array

return 0;
}

Output :
Constructor called!
Constructor called!
Constructor called!
Constructor called!
Destructor called!
Destructor called!
Destructor called!
Destructor called!

MREM (CSE, IT) Page 14


C++ PROGRAMMING LAB

9.write a c++ program to create multilevel inheritance

Aim : implement a c++ program to create multilevel inheritance

// inheritance.cpp
#include <iostream>
using namespace std;
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};

MREM (CSE, IT) Page 15


C++ PROGRAMMING LAB

int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
} //end of program

Output:
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18

MREM (CSE, IT) Page 16


C++ PROGRAMMING LAB

10. write a program to create array of pointers. Invoke functions using array objects.

Aim : implement a program to create array of pointers. Invoke functions using array
objects

Program Code:

#include<stdio.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 D : public A
{
public:
void show()
{
cout<<”D\n”;

MREM (CSE, IT) Page 17


C++ PROGRAMMING LAB

}
};
class E:public A
{
public:
void show()
{
cout<<”E”;
}
};
void main()
{
A a;
B b;
C c;
D d;
E e;
A *pa[]={&a,&b,&c,&d,&e};
for(int j=0;j<5;j++)
pa[j]->show();
}

Output:
A
B
C
D
E

MREM (CSE, IT) Page 18


C++ PROGRAMMING LAB

11. write a c++ program to use pointer for both base and derived classes and call the
member function. Use virtual keyword.

Aim : Implement a c++ program to use pointer for both base and derived classes and call the
member function. Use virtual keyword

Program code:

#include<iostream>
using namespace std;
class base
{
public:
void fun_1() { cout << "base-1\n"; }
virtual void fun_2() { cout << "base-2\n"; }
virtual void fun_3() { cout << "base-3\n"; }
virtual void fun_4() { cout << "base-4\n"; }
};
class derived : public base
{
public:
void fun_1() { cout << "derived-1\n"; }
void fun_2() { cout << "derived-2\n"; }
void fun_4(int x) { cout << "derived-4\n"; }
};
int main()
{
base *p;
derived obj1;
p = &obj1;
// Early binding because fun1() is non-virtual

MREM (CSE, IT) Page 19


C++ PROGRAMMING LAB

// in base
p->fun_1();
// Late binding (RTP)
p->fun_2();
// Late binding (RTP)
p->fun_3();
// Late binding (RTP)
p->fun_4();
// Early binding but this function call is
// illegal(produces error) becasue pointer
// is of base type and function is of
// derived class
//p->fun_4(5);
}

Output :

base-1
derived-2
base-3
base-4

MREM (CSE, IT) Page 20

You might also like