C++ lab program
C++ lab program
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).
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 variables declared at different scope levels.
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
Lab- 1
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";
}
};
int main(){ Student_Info stud[3]; int i;
for(i=0; i<3; i++) stud[i].read_data(i);
cout<<"\n\n+++++++++++++++++++++++++++++++++++++++++++++++\n";
cout<<"The information of 3 students has been saved.";
cout<<"\n+++++++++++++++++++++++++++++++++++++++++++++++\n";
for(i=0; i<3; i++)
stud[i].display_data(i);
return 0 ;
}
OUTPUT:
Lab– 2
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;
cout<<"College Code: ";
cin>>college.college_code;
cout<<"Department: ";
cin>>college.dept;
cout<<"Department In-take: ";
cin>>college.intake;
cout<<"\n\n********* College Information *********\n\n";
cout<<"Name of the college : "<<college.college_name;
cout<<"\nCollege University Code: "<<college.college_code;
cout<<"\nName of the Department: "<<college.dept;
cout<<"\nThe department of "<<college.dept<<" has in-take : "<<college.intake;
cout<<"\n\n \n\n";
return 0;}
}
OUTPUT:
Lab - 3
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;
}
void display(){
int area = getArea();
cout<<"\n*** Rectangle Information ***\n";
cout<<"Length = "<<length; cout<<"\
nBreadth = "<<breadth; cout<<"\nArea =
"<<area;
cout<<"\n \n";
}
};
int main()
{
RectangleTest rect, *class_ptr;
HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
class_ptr = ▭
//Accessing member functions using class pointer
SetConsoleTextAttribute(color, 10 ); //Setting color Green
cout<<"\nUsing member functions access";
SetConsoleTextAttribute(color, 7 ); //Setting color White
class_ptr->initialize(10,5);
class_ptr->display();
//Accessing data members using class pointer
SetConsoleTextAttribute(color, 10 ); //Setting color Green
cout<<"\nUsing data members access";
SetConsoleTextAttribute(color, 7 ); //Setting color White
class_ptr->length = 2;
class_ptr->breadth = 3;
class_ptr->initialize(class_ptr->length,class_ptr->breadth);
class_ptr->display();
return 0;
}
OUTPUT:
Lab - 4
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();
};
void employee :: get_emp_details()
{
cout<<"\nEnter employee number: ";
cin>>emp_number;
cout<<"\nEnter employee name: ";
cin>>emp_name;
cout<<"\nEnter employee basic: ";
cin>>emp_basic;
cout<<"\nEnter employee DA: ";
cin>>emp_da;
cout<<"\nEnter employee IT: ";
cin>>emp_it;
}
float employee :: find_net_salary(float basic, float da, float it)
{
return (basic+da)-it;
}
void employee :: show_emp_details()
{
cout<<"\n\n**** Details of Employee ****";
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nEmployee number : "<<emp_number;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
cout<<"\nIncome Tax : "<<emp_it;
cout<<"\nNet Salary : "<<find_net_salary(emp_basic, emp_da, emp_it);
cout<<"\n \n\n";
}
int main(){
employee emp;
emp.get_emp_details();
emp.show_emp_details();
return 0;
}
OUTPUT:
Lab - 5
PROGRAM:
using namespace std;
#include<iostream>
#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: ";
cin>>emp_number;
cout<<"Employee Name: ";
cin>>emp_name;
cout<<"Basic Salary: ";
cin>>basic;
cout<<"\n---- Employee "<<count<<" Datails are saved----\n\n";
}
float find_net_salary(){
da = basic * 0.52;
gross_salary = basic + da;
it = gross_salary * 0.30;
net_salary = (basic + da) - it;
return net_salary;
}
void display_emp_details(int count){
cout<<"\n\n*** Employee "<<count<<" Details ***\n";
cout<<"\nEmployee Number : "<<emp_number;
cout<<"\nEmployee Name : "<<emp_name;
cout<<"\nNet Salary: "<<net_salary;
cout<<"\n \n";
}
};
int main(){
Employee emp[100];
int number_of_emp, count;
clrscr();
cout<<"\nPlease enter the number of Employees (Max. 100): ";
cin>>number_of_emp;
for(count=0; count< number_of_emp; count++){
emp[count].read_emp_details(count+1);
}
for(count=0; count < number_of_emp; count++){
emp[count].find_net_salary();
}
for(count=0; count < number_of_emp; count++){
emp[count].display_emp_details(count+1);
}
cout<<"\nPress any key to close!!!";
getch();
return 0;
}
OUTPUT:
Lab- 6
PROGRAM:
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<"Press any key: ";
ch = cin.get();
cout << "You have pressed: ";
cout.put(ch);
return 0;
}
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:
6.2) Formatted console IO operations
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:
Lab- 7
PROGRAM:
#include <iostream>
using namespace std;
int my_variable = 10; // Global variable my_variable
int main()
{
int my_variable = 100; // Local variable my_variable
cout << "Value of global my_variable is " << ::my_variable << endl;
cout << "Value of local my_variable is " << my_variable << endl;
return 0;
}
OUTPUT:
OUTPUT:
7 D) Scope Resolution with Multiple Inheritance
#include <iostream>
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;
}
};
int main()
{
DerivedClass obj;
obj.display();
return 0;
}
OUTPUT:
Lab-8
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:
// new with user-defined data type variable (Objects)
#include <iostream>
using namespace std;
class Rectangle{
int length, width;
public:
Rectangle(){
length = 2;
width = 5;
}
Rectangle(int l, int w){
length = l;
width = w;
}
void area(){
cout<< "Area of Rectangle is " << length * width << endl;
}
};
int main()
{
Rectangle *obj_1, *obj_2;
obj_1 = new Rectangle(); // Dynamic memory allocation
obj_2 = new Rectangle(3, 10); // Dynamic memory allocation
obj_1->area();
obj_2->area();
return 0;
}
OUTPUT:
Lab-9
AIM: Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)
.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;
public:
ChildClass_2(){
c = 1000;
}
void show_c(){
cout<< endl << "Inside the ChildClass_2 show_c method!" << endl;
cout<< "value of c is " << c << endl;
}};
int main()
{
ChildClass_2 obj;
obj.show_a();
obj.show_b();
obj.show_c();
return 0;
}
OUTPUT:
Lab-10
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;
}void displayStudentInfo()
{
cout << "Name of the Student : " << stud_name << endl;
cout << "Marks secured : " << marks << endl;
}
};
int main()
{
Student stud[max],*ptr;
int class_size;
ptr=stud;
cout<< "Enter the number of students in the class ( < " << max << "): ";
cin>> class_size;
for( int i=1; i<=class_size; i++ )
{
(ptr+i)->getStudentInfo(i);
}
cout<< endl << "***** Entered student data *****" << endl;
for( int i=1; i<=class_size; i++ )
{
cout << "Student " << i << endl;
(ptr+i)->displayStudentInfo();
}
return 0;
}
OUTPUT:
Lab-11
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();
cout << "Loading gun features.\n";
}
};
class Loader
{
public:
void loadFeatures(Weapon *weapon)
{
weapon->features();
}
};
int main()
{
Loader *l = new Loader;
Weapon *w;
Bomb b;
Gun g;
w = &b;
l->loadFeatures(w);
w = &g;
l->loadFeatures(w);
return 0;
}
OUTPUT: