Lab 10
Lab 10
#include <iostream>
using namespace std;
class student
{
int id;
char name[25];
char subject1[15],subject2[15];
float sub1Mark,sub2Mark;
public:
void GetData() //Statement 1 : Defining GetData()
{
cout<<"\n\tEnter Student Id : ";
cin>>id;
cout<<"\n\tEnter Student Name : ";
cin>>name;
cout<<"\n\tEnter Subject1 Name : ";
cin>>subject1;
cout<<"\n\tEnter Subject2 Name : ";
cin>>subject2;
cout<<"\n\tEnter Subject1 Mark : ";
cin>>sub1Mark;
cout<<"\n\tEnter Subject2 Mark : ";
cin>>sub2Mark;
}
float Total()
{
return sub1Mark+sub2Mark;
}
void PutData() //Statement 2 : Defining PutData()
{
cout<<"\n"<<id<<"\t"<<name<<"\t"<<subject1<<"\t"<<subject2<<"\t"<
<sub1Mark<<"\t"<<sub2Mark<<"\t"<<Total();
}
};
int main() {
int i;
student S[3]; //Statement 3 : Creating Array of 3 Employees
for(i=0;i<3;i++)
{
cout<<"\nEnter details of "<<i+1<<" Student";
S[i].GetData();
}
cout<<"\nDetails of students are:";
for(i=0;i<3;i++)
{
S[i].PutData();
}
return 0;
}
Sample 7-(a) #: Write a program in C++ to create a base class named Rectangle
containing length and width data members. From this class, derive a class named Box
with another data member named depth. The member functions of the base Rectangle
class should consist of a constructor and an area () function. The derived Box class should
have a constructor, a volume () function, and an override function named area () that
returns the surface area of the box.
Note:
Area of rectangle= length * width,
Volume of box=length *width * depth,
Area of Box= (2 * (length + width) * depth) + (2 * length *width)
#include <iostream>
using namespace std;
class Rectangle
{
protected:
int length;
int width;
public:
Rectangle(int a = 0 , int b = 0)
{
length = a ;
width = b ;
}
int area()
{
return (length * width) ;
}
};
public:
Box(int a = 0)
{
depth = a ;
}
voidsetLW(int a , int b)
{
length = a ;
width = b ;
}
int volume()
{
return (Rectangle::area() * depth);
}
int area()
{
return (2 * (length + width) * depth) + (2 * Rectangle::area());
}
};
int main()
{
int x ;
Rectangle obj1(2 , 3);
Box obj2(4) ;
obj2.setLW(3,5);
cout<< obj1.area() << endl;
cout<< obj2.volume() << endl;
cout<< obj2.area() << endl;
return 0 ;
}
Sample 7-(b) #: Write a C++ program to find the volume and the surfacearea of two
BOXES. The program should contain a base class named Rectangle to find the area
and perimeter of the base of each box, and two derived classes: Volume class and
SurfaceArea class.
Note that the volume = base area × height, and
The surface area = (base perimeter × height) + (2 * base area).
#include <iostream>
using namespace std;
class Rectangle
{
protected:
int length;
int width;
public:
int area()
{
return (length * width);
}
voidsetLW(int a , int b)
{
length = a ;
width = b ;
}
int perimeter()
{
return ((length + width) * 2);
}
};
public:
int volume(int height)
{
return (height * Rectangle::area());
}
};
public:
intSArea(int height)
{
return ((height * Rectangle::perimeter()) + (2 * Rectangle::area()));
}
};
int main()
{
Rectangle obj1;
Volume obj2;
SurfaceArea obj3;
obj2.setLW(2 , 3);
obj3.setLW(3 , 4);
return 0 ;