Need assistance with this simple program

This is my first ever OO program so please don't judge me lol. This program is meant to print out the area of a rectangle and triangle with the same width and height. I feel that my classes are correct, but i have no idea how to use them in the main. Thanks for the help in advance :)
Here is the task im trying to complete,
Write a program with a mother class and an inherited daugther class.Both of them should have a method void display ()that prints a message (different for mother and daugther).In the main define a daughter and call the display() method on it.

My code so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// OOP exercises.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

class shape {
	public:
		shape(int height, int width) {
			height = 5;
			width = 4;
		}
};

class rectangle {
	public:
		int area(int height, int width) {
		int area = height * width;
		cout << area << endl;
		}
};

class triangle {
	public:
		int area(int height, int width) {
		int area = height * width / 2;
		cout << area << endl;
		}
};







int main()
{
	
	triangle obj;
	rectangle myObj;



    return 0;
}
Last edited on
Your classes aren't entirely correct. Better would be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
class Shape
{
private:
    int w, h;
public:
private:
    Shape(int width, int height)
    {
        w = width;
        h = height;
    }
};

class Rectangle
{
public:
    Rectangle(int width, int height) : Shape(width, height)
    {
    }
    int area() 
    {
        return width * height;
    }
};
/*Do Triangle class in a similar way, only change the formula for the area*/

int main()
{
    Rectangle rect(5, 4);
    std::cout << rect.area();
}

Topic archived. No new replies allowed.