0% found this document useful (0 votes)
14 views3 pages

Task 2 Program Code

The document defines classes for different shapes including Rectangle, Triangle and Square. The classes inherit from an abstract Shape class and override methods like getArea() and getPerimeter() to calculate dimensions for each shape type. Main tests the classes by creating sample shapes and printing their output.

Uploaded by

neverfinding177
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)
14 views3 pages

Task 2 Program Code

The document defines classes for different shapes including Rectangle, Triangle and Square. The classes inherit from an abstract Shape class and override methods like getArea() and getPerimeter() to calculate dimensions for each shape type. Main tests the classes by creating sample shapes and printing their output.

Uploaded by

neverfinding177
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/ 3

#include <iostream>

using namespace std;

class Shape {
private:
float width;
float height;

public:

virtual float getArea() = 0;

void setWidth(float w) {
width = w;
}
float getWidth() {
return width;
}
void setHeight(float h) {
height = h;
}
float getHeight() {
return height;
}

// constructor blank
Shape() {
height = 0;
width = 0;

}
// constructor args
Shape(float w, float h) {
width = w;
height = h;

}
void print() {
cout << "Shapes width is: " << getWidth();
cout << "Shapes height is: " << getHeight();
}
};

class Rectangle : public Shape {


private:
const string name = "Rectangle";

public:

string getName() {
return name;
}

file:///C/...ments/Alternative%20Assessments%20Green/IY53%20Alternative%20Assessment/Task%202%20Program%20Code%20Yellow.txt[23/04/2021 16:36:17]
float getArea() {
float area = 0;
area = getWidth() * getHeight();
return area;
}

float getPerimeter() {
float perimeter = 0;
perimeter = 2 * getWidth() + 2 * getHeight();
return perimeter;
}
};

class Triangle : public Shape {


private:
const string name = "Triangle";
float sides[2];

public:
string getName() {
return name;
}

float getArea() {
float area = 0;
area = (getWidth() * getHeight()) / 2;
return area;
}

void setSides(float s1, float s2) {


sides[0] = s1;
sides[1] = s2;
}

float* getSides() {
return sides;
}

float getPerimeter() {

float totalSides;
float* p1 = getSides();
totalSides = p1[0] + p1[1];

float perimeter = 0;
perimeter = getWidth() + totalSides;
return perimeter;
}
};

class Square : public Rectangle {

file:///C/...ments/Alternative%20Assessments%20Green/IY53%20Alternative%20Assessment/Task%202%20Program%20Code%20Yellow.txt[23/04/2021 16:36:17]
private:
const string name = "Square";

public:

string getName() {
return name;
}

float getArea() {
float area = 0;
area = getWidth() * getWidth();
return area;
}

float getPerimeter() {
float area = 0;
area = getWidth() * 4;
return area;
}

};

int main() {
Rectangle r1;
r1.setWidth(2);
r1.setHeight(4);
cout << r1.getArea() << endl;
cout << r1.getPerimeter() << endl;
Square s1;
s1.setWidth(5);
cout << s1.getArea() << endl;
cout << s1.getPerimeter() << endl;

file:///C/...ments/Alternative%20Assessments%20Green/IY53%20Alternative%20Assessment/Task%202%20Program%20Code%20Yellow.txt[23/04/2021 16:36:17]

You might also like