"Classes" Practice Example

I need help trying to complete this practice question. I have listed my main function, my header file named "Triangle" and another .cpp file named "Triangle" as well. This is the question being asked:

Design a main function to test the Triangle class in the following way:
1. Declare an object tri of the Triangle. Get the triangle’s width, length and name from the
user, and store them in this object using member functions. Then display the name,
base, height, and area of the tri object.
2. Declare a Triangle pointer triPtr. Dynamically allocate an object belonging to the
Triangle class, assign the values that was read by the user to name, base, and height
using member functions, and then display the name, base, height, and area. At last,
delete dynamically allocated object.

This is what I have so far...
MAIN:
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <cstring>
#include "Triangle.h"

using namespace std;

int main()
{

}

HEADER:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Triangle
{
private:
    double base;
    double height;
    std::string name;
public:
    void setValues(double a, double b);
    void setName(std::string s);
    void getValues();
    void getName();
    double getArea();
};

.cpp:
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
#include "Triangle.h"

void Triangle::setValues(double a, double b)
{
    base = 0;
    height = 0;
}

void Triangle::setName(std::string s)
{
    name = new char[0];
}

void Triangle::getValues()
{
    
}

void Triangle::getName()
{
    
}

double Triangle::getArea()
{
    
    return 0;
}
Last edited on
setName() should set the name to the parameter s that you passed in.
getName() should return a string that is the name of the triangle.
Replace getValues() with getBase() and getHeight(). These should return doubles.
getArea() should return the area. You'll have to compute this from the base and height. If you don't know how, look it up.
Final Result

MAIN:
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
#include <iostream>
#include <cstring>
#include "Triangle.h"

using namespace std;

int main()
{
    Triangle Tri;                           //Listing Variables
    double base, height;
    string name;
    Triangle *triPtr = new Triangle;        //Making new triangle a pointer
    
    cout << "Enter a base for your Triangle: \n";
    cin >> base;
    cout << "Enter a height for your Triangle: \n";
    cin >> height;
    cout << "Enter a name for your Triangle: \n";
    cin >> name;
    cout << endl;//clean up the output more
    Tri.setValues(base, height);            //Calling to Triangle.cpp functions
    Tri.setName(name);                      //and running them
    Tri.getValues();
    Tri.getName();
    cout << "The area of your Triangle = " << Tri.getArea() << endl << endl;
//Two endl this way the output is neater(my own preference)
    
    cout << "Enter a base for your Triangle: \n";
    cin >> base;
    cout << "Enter a height for your Triangle: \n";
    cin >> height;
    cout << "Enter a name for your Triangle: \n";
    cin >> name;
    cout << endl;
    triPtr->setValues(base, height);       //pointing to functions in Triangle.cpp
    triPtr->setName(name);
    triPtr->getValues();
    triPtr->getName();
    cout << "The area of your Triangle = " << triPtr->getArea() << endl;
    delete triPtr;                         //deleting from memory
    
    return 0;
}

HEADER:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>  //to use std::string
class Triangle              //creating class Triangle
{
private:                    //making variable private
    double base;
    double height;
    std::string name;
public:                     //making variables public
    void setValues(double a, double b);
    void setName(std::string s);
    void getValues();
    void getName();
    double getArea();
};

TRIANGLE.cpp:
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
#include "Triangle.h"
using namespace std;

void Triangle::setValues(double a, double b)   //Setting all functions for use
{                                              //in the main function
    base = a;       //making base and height double values
    height = b;
}

void Triangle::setName(std::string s)
{
    name = s;       //making name a string to be called on
}

void Triangle::getValues()
{
    cout << "Base of the Triangle = " << base << endl;
    cout << "Height of the Triangle = " << height << endl;
}

void Triangle::getName()
{
    cout << "Name of the Triangle = " << name << endl;
}

double Triangle::getArea()
{
    return (0.5)*base*height;               //returning area of a triangle using
}                                           //the formula (1/2)(base)(height) 
Your code works, but please let me suggest some improvements.

Since triangle.h doesn't need iostream, you shouldn't include it there. Instead, include it in triangle.cpp and main.cpp. Even though both of those files need it, that won't always be the case. In general, a header file should #include exactly the headers that it needs to avoid compilation errors, nothing more and nothing less.

Your getName() and getValues() functions don't do what most programmers will expect. Generally method getXYZ() returns a copy of (or reference to) member value XYZ, or computes value XYZ from the members. So one would expect getName() to be std::string getName() { return name; } This is so common that deviating from the pattern is a bad idea.

One alternative is to create getName(), getHeight() and getBase() methods, then use then within main() to print the value of the two triangles. Yuck. That will be a bunch of duplicated code in main(). So I'd suggest a display() method. You don't actually need to retrieve the base, height or name inside main(), you just need to display the info about two triangles.

Modifying your code and putting it all in one file for my convenience:

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string>

class Triangle              //creating class Triangle
{
private:                    //making variable private
    double base;
    double height;
    std::string name;
public:                     //making variables public
    void setValues(double a, double b);
    void setName(std::string s);
    double getArea();
    void display();
};

#include <iostream>
using namespace std;

void Triangle::setValues(double a, double b)   //Setting all functions for use
{                                              //in the main function
    base = a;       //making base and height double values
    height = b;
}

void Triangle::setName(std::string s)
{
    name = s;       //making name a string to be called on
}

void Triangle::display()
{
    cout << "Name of the Triangle = " << name << endl;
    cout << "Base of the Triangle = " << base << endl;
    cout << "Height of the Triangle = " << height << endl;
    cout << "Area of the Triangle = " << getArea() << endl << endl;
}

double Triangle::getArea()
{
    return (0.5)*base*height;               //returning area of a triangle using
}                                           //the formula (1/2)(base)(height) 

#include <iostream>
#include <cstring>
// #include "Triangle.h"

using namespace std;

int main()
{
    Triangle Tri;                           //Listing Variables
    double base, height;
    string name;
    Triangle *triPtr = new Triangle;        //Making new triangle a pointer
    
    cout << "Enter a base for your Triangle: \n";
    cin >> base;
    cout << "Enter a height for your Triangle: \n";
    cin >> height;
    cout << "Enter a name for your Triangle: \n";
    cin >> name;
    cout << endl;//clean up the output more
    Tri.setValues(base, height);            //Calling to Triangle.cpp functions
    Tri.setName(name);                      //and running them
    Tri.display();
    
    cout << "Enter a base for your Triangle: \n";
    cin >> base;
    cout << "Enter a height for your Triangle: \n";
    cin >> height;
    cout << "Enter a name for your Triangle: \n";
    cin >> name;
    cout << endl;
    triPtr->setValues(base, height);       //pointing to functions in Triangle.cpp
    triPtr->setName(name);
    triPtr->display();
    delete triPtr;                         //deleting from memory
    
    return 0;
}

Topic archived. No new replies allowed.