Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sys 4

Download as pdf or txt
Download as pdf or txt
You are on page 1of 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

//RA2311043010090
#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:
//RA2311043010090
#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