C++ Programming Lab Manual: Sri Indu Institute of Engineering &technology
C++ Programming Lab Manual: Sri Indu Institute of Engineering &technology
A.Y:2019-2020(R18)
AIM: Write a C++ program to display names, roll no and grades of 3 students whohave
appeared in examination. declare the class of name, roll no and grade. create an array of
class objects .read and display the contents of array.
PROCEDURE:
Step 1 - Include the required header files (iostream.h and conio.h).
Step 2 - Create a class (StudentInfo) with the following class members as public members.
student_name, roll_number and grade as data members.
read_info() and display_info() as member functions.
Step 3 - Implement the read_info() and display_info() member functions.
Step 4 - Create a main() method.
Step 5 - Create an object of the above class inside the main() method.
Step 6 - Make function calls to read_info() and display_info() using class object.
PROGRAM:
#include <iostream>
using namespace std;
class Student_Info{
int roll_number;
char student_name[50], grade[2];
public:
void read_data(int count){
cout<<"\n\n--------- Enter student "<<count+1<<" information ---------\n";
cout<<"Name of the Student (Max. 50 characters only): ";
cin>>student_name;
cout<<"Roll Number: ";
cin>>roll_number;
cout<<"Grade (O, A+, A, B+, B, C, D, F): ";
cin>>grade;
cout<<"\nStudent information with roll number "<<roll_number<<" has saved!";
}
void display_data(int count){
cout<<"\n\n******** Student "<<count+1<<" Information ********";
cout<<"\nName of the Student: "<<student_name;
cout<<"\nRoll Number: "<<roll_number;
cout<<"\nGrade Secured: "<<grade;
cout<<"\n---------------------------------------\n";
AIM: Write a c++ program to declare struct, intialize and display contents of member
varaiables
PROCEDURE:
PROGRAM:
#include <iostream>
using namespace std;
struct college_info{
char college_name[15];
char college_code[2];
char dept[50];
int intake;
};
int main()
{
struct college_info college;
cout<<"\n+++++ Enter the College Information +++++\n\n";
cout<<"Name of the college: ";
cin>>college.college_name;
OUTPUT:
AIM: Write a C++ program to declare a class, declare pointer to class, initailize and display
contents of class member.
PROCEDURE:
• Step 1 - Include the required header files (iostream.h, conio.h, and windows.h for colors).
• Step 2 - Create a class (Rectangle) with the following members as public members. length
and breadth as data members. initialize(), getArea() and display() as member functions.
• Step 3 - Create a main() method.
• Step 4 - Create a variable (rect) and a pointer variable (class_ptr) of the above class inside
the main() method.
• Step 5 - Assign the address of class object (rect) to class pointer object (class_ptr).
• Step 6 - Then, call the member functions initialize() and display() using class pointer object
(class_ptr) to illustrate member functions access using class pointer.
• Step 7 - Assign values to data members of the class using class pointer object to illustrate
data members access using class pointer.
• Step 8 - Finally, call the member functions initialize() and display() using class pointer
object (class_ptr).
PROGRAM:
#include <windows.h>
#include <iostream>
using namespace std;
class RectangleTest{
public:
int length, breadth;
public:
void initialize(int len, int bre){
length = len;
breadth = bre;
}
int getArea(){
return 2*length*breadth;
OUTPUT:
AIM: Given that an employee class contains following members, data members, employee
number, employee name ,basic, DA, IT, net salary and print data members
PROCEDURE:
• Step 1 - Include the required header files (iostream.h, conio.h, and windows.h for colors).
• Step 2 - Create a class (employee) with the following members as public members.
emp_number, emp_name, emp_basic, emp_da, emp_it, and emp_net_sal as data members.
get_emp_details(), find_net_salary() and show_emp_details() as member functions.
• Step 3 - Implement all the member functions with their respective code (Here, we have
used scope resolution operator ::).
• Step 3 - Create a main() method.
• Step 4 - Create an object (emp) of the above class inside the main() method.
• Step 5 - Call the member functions get_emp_details() and show_emp_details().
• Step 6 - returnn 0 to exit form the program execution.
PROGRAM:
#include <windows.h>
#include <iostream>
using namespace std;
class employee
{
int emp_number;
char emp_name[20];
float emp_basic;
float emp_da;
float emp_it;
float emp_net_sal;
public:
void get_emp_details();
float find_net_salary(float basic, float da, float it);
void show_emp_details();
OUTPUT:
AIM: 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 gross salary.
PROCEDURE:
• Step 1 - Include the required header files (iostream.h, conio.h, and windows.h for colors).
• Step 2 - Create a class Employee with the following members. emp_number, emp_name,
basic, da, it, gross_salary, and net_salary as data members read_emp_details(),
find_net_salary(), and display_emp_details() as member functions.
• Step 3 - Implement all the member functions with their respective code.
• Step 4 - Create a main() method.
• Step 5 - Create an array of class object with a specific size and number_of_emp as integer.
• Step 6 - Read number_of_emp.
• Step 7 - Call the read_emp_details() method through the array of class object from 0 to
number_of_emp.
• Step 8 - Call the find_net_salary() method through the array of class object from 0 to
number_of_emp.
• Step 9 - Call the display_emp_details() method through the array of class object from 0 to
number_of_emp.
• Step 10 - returnn 0 to exit form the program execution.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class Employee
{
char emp_name[30];
int emp_number;
float basic, da, it, gross_salary, net_salary;
public:
void read_emp_details(int count){
cout<<"\n\n*** Enter Employee "<<count<<" Details ***";
cout<<"\nEmployee Number: ";
We use the following built-in functions to perform operations of type unformatted consol IO
operations.
OUTPUT:
#include <iostream>
using namespace std;
int main()
{
char ch[20];
cout<<"What is your favourite website: ";
cin.getline(ch, 20);
cout <<endl<< "visit: www.";
cout.write(ch, 17);
cout<<".com"<<endl;
return 0;
OUTPUT:
OUTPUT:
The C++ programming language provides the following built-in functions to display the output in
formatted form. These built-in functions are available in the header file iomanip.h.
PROGRAM:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(10);
cout<<x<<endl;
cout<<setw(10)<<setfill('*')<<x<<endl;
return 0;
}
OUTPUT:
AIM: Write a C++ program to use scope resolution operator. Display the various values of
the same variables declared at different scope levels.
OUTPUT:
OUTPUT:
class BaseClass_1{
public:
int var_a = 10;
void display(){
cout<<"I am in BaseClass display()"<<endl;
cout<<"var_a = "<<var_a;
}
};
class BaseClass_2{
public:
int var_a = 20;
void display(){
cout<<"I am in BaseClass_2 display()"<<endl;
cout<<"var_a = "<<var_a;
}
};
class DerivedClass:public BaseClass_1, public BaseClass_2{
public:
int var_a = 30;
void display(){
cout<<"I am in DerivedClass display()"<<endl<<endl;
cout<<"BaseClass_1 var_a = "<<BaseClass_1::var_a<<endl;
cout<<"BaseClass_2 var_a = "<<BaseClass_2::var_a<<endl;
cout<<"DerivedClass var_a = "<<var_a<<endl;
OUTPUT:
PROGRAM:
#include <iostream>
using namespace std;
int main()
{ int *ptr;
ptr = new int; // Dynamic memory allocation
cout<< "Number of bytes allocated to ptr is " << sizeof(ptr) << endl;
*ptr = 100;
cout << "Value at ptr is " << *ptr << endl;
return 0;
}
OUTPUT:
.PROGRAM:
#include <iostream>
using namespace std;
class ParentClass{
int a;
public:
ParentClass(){
a = 10;
}
void show_a(){
cout<< endl << "Inside the ParentClass show_a method!" << endl;
cout<< "value of a is " << a << endl;
}};
class ChildClass_1:public ParentClass{
int b;
public:
ChildClass_1(){
b = 100;
}
void show_b(){
cout<< endl << "Inside the ChildClass_1 show_b method!" << endl;
cout<< "value of b is " << b << endl;
}
};
class ChildClass_2:public ChildClass_1{
int c;
OUTPUT:
PROCEDURE:
PROGRAM:
#include <iostream>
using namespace std;
#define max 100
class Student
{
string stud_name;
int marks;
public:
void getStudentInfo(int i)
{
cout<< endl << "Enter the student " << i << " details" << endl;
cout<< "Name of the Student: ";
cin>> stud_name;
cout<< "Marks secured: ";
cin>> marks;
}
PROGRAM:
#include <iostream>
using namespace std;
class Weapon
{
public:
virtual void features()
{
cout << "Loading weapon features.\n";
}
};
class Bomb : public Weapon
{
public:
void features()
{
this->Weapon::features();
cout << "Loading bomb features.\n";
}
};
class Gun : public Weapon
{
public:
void features()
{
this->Weapon::features();
#include <iostream>
using namespace std;
class operation
{
int a,b,add,sub,mul;
float div;
public:
void get();
void sum();
void difference();
void product();
void division();
};
inline void operation :: get()
{
cout << "Enter first value:";
cin >> a;
cout << "Enter second value:";
cin >> b;
}
int main()
{
cout << "Program using inline function\n";
operation s;
s.get();
s.sum();
s.difference();
s.product();
s.division();
return 0;
}
OUTPUT:
#include <iostream>
public:
int x;
void getdata()
private:
int y;
public:
void readdata()
void product()
};
int main()
a.getdata();
a.readdata();
a.product();
return 0;
Product = 12
#include<iostream.h>
#include<conio.h>
void main()
int a=2;
try
if(a==1)
else if(a==2)
else if(a==3)
catch(int a)
catch(char ch)
catch(double d)
cout<<"\nEnd of program.";
OUTPUT:
End of program.
#include< iostream.h>
#include< iomanip.h>
OUTPUT:
Enter number
100
Hexadecimal base =64
Octal base =144
hexadecimal base = 64
Octal base = 144
**350**200
***350***200
****350****200
1.75