0% found this document useful (0 votes)
9 views8 pages

Lab 10

The document provides C++ program examples demonstrating the use of classes to calculate geometric properties such as the volume of a sphere, area of a circle, total marks of students, and volume and surface area of boxes. It includes class definitions with data members and member functions for each geometric shape and student details. Additionally, it showcases inheritance with base and derived classes for rectangles and boxes.

Uploaded by

riadxx1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Lab 10

The document provides C++ program examples demonstrating the use of classes to calculate geometric properties such as the volume of a sphere, area of a circle, total marks of students, and volume and surface area of boxes. It includes class definitions with data members and member functions for each geometric shape and student details. Additionally, it showcases inheritance with base and derived classes for rectangles and boxes.

Uploaded by

riadxx1122
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Write a program in C++ using class to calculate the Volume of the Sphere as per following

diagram descriptions: (Volume = 4 𝜋 r2)

Class Name : 𝑆𝑝ℎ𝑒𝑟𝑒


Data Members:
- 𝑟𝑎𝑑𝑖𝑢𝑠: 𝑑𝑜𝑢𝑏𝑙𝑒
- volume: double

+ 𝑆𝑝ℎ𝑒𝑟𝑒 ( ) //𝐷𝑒𝑓𝑎𝑢𝑙𝑡 𝑐𝑜𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑜𝑟


+ 𝐺𝑒𝑡𝐷𝑎𝑡𝑎 ( ) //𝑀𝑒𝑚𝑏𝑒𝑟𝑓𝑢𝑛𝑐𝑡𝑖𝑜𝑛 𝑡𝑜𝑔𝑒𝑡 𝑑𝑎𝑡𝑎
+ 𝑉𝑜𝑙𝑢𝑚𝑒 ( ) ∶ 𝑑𝑜𝑢𝑏𝑙𝑒 // 𝑟𝑒𝑡𝑢𝑟𝑛 𝑣𝑜𝑙𝑢𝑚𝑒 𝑜𝑓 𝑡ℎ𝑒 𝑆𝑝ℎ𝑒𝑟𝑒
Sample 5-(b) #: Write a program in C++ using class to calculate the Area of the Circle as
per following diagram descriptions: (Area = 𝜋 r2)

Class Name : 𝐶𝑖𝑟𝑐𝑙𝑒


Data Members:
- 𝑟𝑎𝑑𝑖𝑢𝑠: 𝑑𝑜𝑢𝑏𝑙𝑒
- area: double

+ 𝐶𝑖𝑟𝑐𝑙𝑒 (𝑑𝑜𝑢𝑏𝑙𝑒 𝑟 ) // 𝑝𝑎𝑟𝑎𝑚𝑒𝑡𝑒𝑟𝑖𝑧𝑒𝑑 𝑐𝑜𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑜𝑟


+ 𝐴𝑟𝑒𝑎 ( ) ∶ 𝑑𝑜𝑢𝑏𝑙𝑒 // 𝑟𝑒𝑡𝑢𝑟𝑛 𝑣𝑜𝑙𝑢𝑚𝑒 𝑜𝑓 𝑡ℎ𝑒 𝑆𝑝ℎ𝑒𝑟𝑒
Sample 6(b)#: Write a C++ program using array of objects to calculate the total marks
(Subject1 Mark+ Subject2 Mark) of three students. The details of students (id: type int, name:
type char [25], subject1 mark: type float, subject2 mark: type float) should be entered by the
user and display as given below;
Details of Students
Total Marks Subject1Mark Subject2Mark Name ID

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

};

class Box : public Rectangle


{
private:
int depth ;

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

class Volume : public Rectangle


{
private:

public:
int volume(int height)
{
return (height * Rectangle::area());
}
};

class SurfaceArea : public Rectangle


{
private:

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

cout<< obj2.volume(5) << endl;


cout<< obj3.SArea(5) << endl;

return 0 ;

You might also like