0% found this document useful (0 votes)
19 views

Darshan Sys 4

The document describes two C++ programs that demonstrate the use of classes: 1. A program that creates a Room class with functions to calculate area and volume. It takes user input for room dimensions and displays the calculated area and volume. 2. A program that implements hierarchical inheritance to calculate the square and cube of a number. It defines a base Number class and derived Square and Cube classes, takes user input, creates objects of the derived classes, and displays the square and cube of the number.

Uploaded by

sv8049
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)
19 views

Darshan Sys 4

The document describes two C++ programs that demonstrate the use of classes: 1. A program that creates a Room class with functions to calculate area and volume. It takes user input for room dimensions and displays the calculated area and volume. 2. A program that implements hierarchical inheritance to calculate the square and cube of a number. It defines a base Number class and derived Square and Cube classes, takes user input, creates objects of the derived classes, and displays the square and cube of the number.

Uploaded by

sv8049
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/ 5

EXPERIMENT 4

DEVELOP AND PRACTICE C++ APPLICATION PROGRAMS USING


CLASSES
AIM/OBJECTIVE: To write and execute programs in C++ to demonstrate the use of
classes
SOFTWARE USED: Online C++ Compiler
PROGRAMS:

1. Write a C++ program to create a class name “room” andwrite member


functions to calculate the area and volume of the room

//RA2311043010077
#include <iostream>

class Room {
private:
double length;
double width;
double height;

public:
// Constructor
Room(double l, double w, double h) : length(l), width(w), height(h) {}

// Function to calculate area


double calculateArea() {
return 2 * (length * width + length * height + width * height);
}

// Function to calculate volume


double calculateVolume() {
return length * width * height;
}
};

int main() {
// Taking input for dimensions of the room
double length, width, height;
std::cout << "Enter the length of the room: ";
std::cin >> length;
std::cout << "Enter the width of the room: ";
std::cin >> width;
std::cout << "Enter the height of the room: ";
std::cin >> height;

// Creating Room object


Room room(length, width, height);

// Calculating and displaying area and volume of the room


std::cout << "Area of the room: " << room.calculateArea() << " square units" <<
std::endl;
std::cout << "Volume of the room: " << room.calculateVolume() << " cubic units" <<
std::endl;

return 0;
}

OUTPUT:
2. Write a C++ program for hierarchical inheritance to get square and cube of a
number.

Program code:
//RA2311043010077
#include <iostream>

// Base class
class Number {
protected:
int num;

public:
// Constructor
Number(int n) : num(n) {}

// Function to get the number


int getNumber() {
return num;
}
};
// Derived class for computing square
class Square : public Number {
public:
// Constructor
Square(int n) : Number(n) {}

// Function to compute square


int getSquare() {
return num * num;
}
};

// Derived class for computing cube


class Cube : public Number {
public:
// Constructor
Cube(int n) : Number(n) {}

// Function to compute cube


int getCube() {
return num * num * num;
}
};

int main() {
int number;
std::cout << "Enter a number: ";
std::cin >> number;

// Creating objects of derived classes


Square squareObj(number);
Cube cubeObj(number);

// Displaying square and cube of the number


std::cout << "Square of " << number << " is: " << squareObj.getSquare() << std::endl;
std::cout << "Cube of " << number << " is: " << cubeObj.getCube() << std::endl;
return 0;
}

OUTPUT:

You might also like