C++ Lab r18
C++ Lab r18
LIST OF EXPERIMENTS
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 Strut. 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).
7. Write a C++ program to use scope resolution operator. Display the various values of the same
variables declared at different scope levels.
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
#include <iostream>
struct student
char name[50];
int roll;
float marks;
} s[10];
int main()
// storing information
s[i].roll = i+1;
cout << "For roll number" << s[i].roll << "," << endl;
cout << "Enter name: ";
// Displaying information
return 0;
}
Output
Enter marks: 98
Enter marks: 89
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
.
Aim: 2. Write a C++ program to declare Strut. Initialize and display contents of member
variables.
#include <iostream>
struct student
char name[50];
int roll;
float marks;
};
int main()
student s;
return 0;
Output
Enter information,
Displaying Information,
Name: Bill
Roll: 4
Marks: 55.6
Aim : 3. Write a C++ program to declare a class. Declare pointer to class. Initialize and display
the contents of the class member
#include <iostream>
class Box {
public:
// Constructor definition
length = l;
breadth = b;
height = h;
double Volume() {
private:
};
int main(void) {
ptrBox = &Box1;
ptrBox = &Box2;
return 0;
When the above code is compiled and executed, it produces the following result −
Constructor called.
Constructor called.
Aim : 4. Given that an EMPLOYEE class contains following members: data members: Employee
number, Employee name, Basic, DA, IT, Net Salary and print data members.
#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();
};
cin>>emp_num;
cout<<"\nEnter employee name:\n";
cin>>emp_name;
cin>>emp_basic;
emp_da=0.52*emp_basic;
emp_it=0.30*(emp_basic+emp_da);
net_sal=(emp_basic+emp_da)-emp_it;
cout<<"\n\n\nDetails of : "<<emp_name;
cout<<"\nEmployee DA : "<<emp_da;
int main()
{
employee emp[10];
int i,num;
clrscr();
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;
Aim : 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
#include<iostream.h>
#include<conio.h>
#define SIZE 5
class emp
float basic,da,it,netsal;
char name[20],num[10];
public:
void getdata();
void net_sal();
void dispdata();
};
void emp::getdata()
cin>>name;
cin>>num;
cin>>basic;
}
void emp::net_sal()
da=((0.52)*basic );
float gsal=da+basic;
it=((0.3)*gsal);
netsal=gsal-it;
void emp::dispdata()
cout
void main()
clrscr();
emp ob[SIZE];
int n;
cout<<"\n\n***********************************"
cin>>n;
for(int i=0;i<n;i++)
ob[i].getdata();
ob[i].net_sal();
clrscr();
cout<<"\n-----------------"
<<"\nEmployee Detail::"
<<"\n-----------------";
for( i=0;i<n;i++)
cout<<"\n\n Employee:"<<i+1
<<"\n ----------";
ob[i].dispdata();
getch();
Out put:
Aim : 6 Write a C++ to illustrate the concepts of console I/O operations
In case of C++ it uses streams to perform input and output operations in standard input output
devices (keyboard and monitor). A stream is an object which can either insert or extract the
character from it.
The standard C++ library is iostream and standard input / output functions in C++ are:
Cin
cout
These input / output operations are in unformatted mode. The following are operations of
unformatted consol input / output operations:
A) void get()
It is a method of cin object used to input a single character from keyboard. But its main
property is that it allows wide spaces and newline character.
Syntax:
char c=cin.get();
Example:
Copy
#include<iostream>
int main()
char c=cin.get();
cout<<c<<endl;
return 0;
Output
B) void put()
It is a method of cout object and it is used to print the specified character on the screen or
monitor.
Syntax:
cout.put(variable / character);
Example:
Copy
#include<iostream>
int main()
char c=cin.get();
return 0;
Output
Aim: 7 write a C++ program to use scope resolution operator. Display the various values of the
filter_none
edit
play_arrow
brightness_4
#include<iostream>
int x; // Global x
int main()
return 0;
Output:
Value of global x is 0
Value of local x is 10
2) To define a function outside a class.
filter_none
edit
play_arrow
brightness_4
#include<iostream>
class A
public:
// Only declaration
void fun();
};
void A::fun()
}
int main()
A a;
a.fun();
return 0;
Output:
fun() called
filter_none
edit
play_arrow
brightness_4
#include<iostream>
class Test
{
static int x;
public:
static int y;
void func(int x)
};
// like this
int Test::x = 1;
int Test::y = 2;
int main()
{
Test obj;
int x = 3 ;
obj.func(x);
return 0;
Output:
Value of static x is 1
Value of local x is 3
Test::y = 2;
#include <iostream>
int main ()
int* p = NULL;
if (!p)
else
*p = 29;
int n = 5;
if (!q)
else
q[i] = i+1;
cout << "Value store in block of memory: ";
delete p;
delete r;
delete[] q;
return 0;
Output:
Value of p: 29
Value of r: 75.25
Aim: 9. Write a C++ program to create multilevel inheritance. (Hint: Classes A1, A2, A3)
// inheritance.cpp
#include <iostream>
public:
int x;
void getdata()
};
public:
int y;
void readdata()
};
private:
int z;
public:
void indata()
void product()
};
int main()
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
Aim: 10. Write a C++ program to create an array of pointers. Invoke functions using array
objects
#include<stdio.h>
int main()
// Pointer to an integer
int *p;
int (*ptr)[5];
int arr[5];
p = arr;
ptr = &arr;
p++;
ptr++;
printf("p = %p, ptr = %p\n", p, ptr);
return 0;
Output:
Aim: 11. Write a C++ program to use pointer for both base and derived classes and call the
member function. Use Virtual keyword
#include<iostream>
class base
public:
void show ()
public:
void print ()
void show ()
};
int main()
base *bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
Output: