0% found this document useful (0 votes)
16 views2 pages

Lab No 2

The document discusses classes in C++ including base and abstract classes. It defines an abstract Shape class and derives Square and Circle classes from it. The main function gets dimensions from the user and calculates and prints the areas of a square and circle by calling methods on the derived classes.

Uploaded by

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

Lab No 2

The document discusses classes in C++ including base and abstract classes. It defines an abstract Shape class and derives Square and Circle classes from it. The main function gets dimensions from the user and calculates and prints the areas of a square and circle by calling methods on the derived classes.

Uploaded by

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

Lab no 2

CLASSES
Objective:
In this lab we study about classes. Base class and Abstract class .we implement
them in our program .

#include <iostream>
using namespace std;
// Abstract class
class Shape {
protected:
float dimension;
public:
void getDimension() {
cin >> dimension;
}
// pure virtual Function
virtual float calculateArea() = 0;
};
// Derived class
class Square : public Shape {
public:
float calculateArea() {
return dimension * dimension;
}
};
// Derived class
class Circle : public Shape {
public:
float calculateArea() {
return 3.14 * dimension * dimension;
}
};

int main()
{
Square square;
Circle circle;

cout << "Enter the length of the square: ";


square.getDimension();
cout << "Area of square: " << square.calculateArea() << endl;

cout << "\nEnter radius of the circle: ";


circle.getDimension();
cout << "Area of circle: " << circle.calculateArea() << endl;

return 0;
}

You might also like