0% found this document useful (0 votes)
307 views24 pages

C++ Programming Lab

The document contains the aims and programs for various C++ programming lab exercises including: 1. A program to get student details using a class and array of objects. 2. A program to create an EMPLOYEE class to calculate salaries. 3. A program using a struct to store and display student details. 4. A program demonstrating a class and pointer to class. 5. A program using the scope resolution operator. 6. A program demonstrating multilevel inheritance with classes A1, A2, and A3.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
307 views24 pages

C++ Programming Lab

The document contains the aims and programs for various C++ programming lab exercises including: 1. A program to get student details using a class and array of objects. 2. A program to create an EMPLOYEE class to calculate salaries. 3. A program using a struct to store and display student details. 4. A program demonstrating a class and pointer to class. 5. A program using the scope resolution operator. 6. A program demonstrating multilevel inheritance with classes A1, A2, and A3.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 24

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;

cout<<"\nIncome Tax : "<<emp_it;


cout<<"\nNet Salary : "<<net_sal;
cout<<endl;
}
int main()
{
employee emp[10];
int i,num;
cout<<"Enter number of employee:";
cin>>num;
cout<<endl;
for(i=0;i<num;i++)
{
cout<<"enter the details of employee:"<<i+1<<endl;
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();
return 0;
}
AIM: Write a C++ program to declare Struct. Initialize and display contents of member variables
PROGRAM:
#include <iostream>
using namespace std;
struct student
{
char name[50];
int roll;
float marks;
};
int main()
{
int i,n;
student s[10];
cout<<"enter number of students:";
cin>>n;
cout<<endl;
cout << "Enter the information of students"<< endl<<endl;
for(i=0;i<n;i++)
{
cout<<"enter the details of student "<<i+1<<":"<<endl;
cout << "Enter name: ";
cin >> s[i].name;
cout << "Enter roll number: ";
cin >> s[i].roll;
cout << "Enter marks: ";
cin >> s[i].marks;
cout<<endl;
}
cout << "\nDisplaying the information of students" << endl<<endl;
for(i=0;i<n;i++)
{
cout<<"Details of student "<<i+1<<":"<<endl;
cout << "Name: " << s[i].name << endl;
cout << "Roll: " << s[i].roll << endl;
cout << "Marks: " << s[i].marks << endl;
}
return 0;
}
AIM: Write a C++ program to declare a class. Declare pointer to class. Initialize and display the
contents of the class member.

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;
}

You might also like