C++ Programming Lab
C++ Programming Lab
C++ Programming Lab
List of Programs:
C++ program to get student details by creating array of class objects.
C++ program to create an class EMPLOYEE and computing employee’s net salary.
C++ program to declare struct and displaying contents of member variables.
C++ program to declare a class and declaring pointer to a class.
C++ program to use scope resolution operator and show that scope resolution operator :: is
used to define a function outside a class.
C++ program to create multilevel inheritance.
AIM: Write a C++ program to display names,Roll no.,and grade 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.
PROGRAM:
#include <iostream>
using namespace std;
#define MAX 10
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student's details
void getDetails(void);
//member function to print student's details
void putDetails(void);
};
//member function definition, outside of the class
void student::getDetails(void)
{
cout << "Enter name: " ;
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter total marks out of 500: ";
cin >> total;
perc=(float)total/500*100;
}
//member function definition, outside of the class
void student::putDetails(void)
{
cout << "Name:"<< name<<endl<< "Roll Number:" << rollNo<<endl << "Total:" << total
<<endl<< "Percentage:" << perc<<endl;
}
int main()
{
student std[MAX]; //array of objects creation
int n,i;
cout << "Enter total number of students: ";
cin >> n;
for(i=0;i< n; i++)
{
cout << "Enter details of student " << i+1 << ":\n";
std[i].getDetails();
}
cout << endl;
for(i=0;i< n; i++)
{
cout << "Details of student " << (i+1) << ":\n";
std[i].putDetails();
}
return 0;
}
AIM:Write a C++ program to create an EMPLOYEE class contains following members: data
members: Employee number, Employee name, Basic, DA, IT, Net Salary and print data members and
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).
PROGRAM:
#include<iostream>
using namespace std;
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<<"Enter employee number:";
cin>>emp_num;
cout<<"Enter employee name:";
cin>>emp_name;
cout<<"Enter employee basic:";
cin>>emp_basic;
cout<<endl;
}
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;
}
void employee :: show_emp_details()
{
cout<<"\nDetails of : "<<emp_name;
cout<<"\nEmployee number : "<<emp_num;
cout<<"\nBasic salary : "<<emp_basic;
cout<<"\nEmployee DA : "<<emp_da;
PROGRAM:
#include <iostream>
using namespace std;
class Box {
public:
void readDim()
{
cout<<"enter the length :";
cin>>length;
cout<<"enter the breadth :";
cin>>breadth;
cout<<"enter the height :";
cin>>height;
}
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() {
Box Box1;
Box Box2;
cout<<"Enter the Dimensions of Box1 object:"<<endl;
Box1.readDim();
cout<<"Enter the Dimensions of Box2 object:"<<endl;
Box2.readDim();
cout<<endl;
Box *ptrBox;
ptrBox = &Box1;
cout << "Volume of Box1: " << ptrBox->Volume() << endl;
ptrBox = &Box2;
cout << "Volume of Box2: " << ptrBox->Volume() << endl;
return 0;
}
AIM: Write a C++ program to use scope resolution operator. Display the various values of the
same variables declared at different scope levels.
PROGRAM:
#include <iostream>
using namespace std;
char c = 'a'; // global variable
int main()
{
char c = 'b'; //local variable
cout << "Local variable: " << c << "\n";
cout << "Global variable: " << ::c << "\n"; //using scope resolution operator
return 0;
}
AIM: Write a C++ program to show that scope resolution operator :: is used to define a function
outside a class
PROGRAM:
#include<iostream>
using namespace std;
class ScopeExample
{
public:
// Only declaration
void hi();
};
// Definition outside class using ::
void ScopeExample::hi()
{
cout << "hi() function called";
}
int main()
{
ScopeExample a;
a.hi();
return 0;
}
AIM: Write a C++ program to create multilevel inheritance.(Hint:class
A1,A2,A3.)
PROGRAM:
#include <iostream>
using namespace std;
class A1
{
public:
int x;
void getx()
{
cout<<"Enter the value of x:"<<endl;
cin>>x;
}
};
class A2:public A1
{
public:
int y;
void gety()
{
cout<<"Enter the value of y:"<<endl;
cin>>y;
}
};
class A3:public A2
{
int z;
public:
void getz()
{
cout<<"Enter the value of z:"<<endl;
cin>>z;
}
void product()
{
cout<<"The product is:"<<x*y*z<<endl;
}
};
int main()
{
A3 a;
a.getx();
a.gety();
a.getz();
a.product();
return 0;
}