Hey guys,
This is my first time posting here so hopefully I'll be understandable. I'm taking CS 1410, which is C++. Here are the instructions for the lab.
-It should create an object of your rectangle class.
-Use the parameterized constructor and pass in the values of 4 and 7 as the height and width.
-Using getHeight and getWidth, display the size of the rectangle represented by your Rectangle object.
-Using the getArea function, display the area of the rectangle object.
Here is my code:
MyRectangle.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include "MyRectangle.h" //we #include the header file for the MyRectangle Class
#include <iostream>
#include<string>
using namespace std;
MyRectangle rectangle(int,int); //make an instance of the myRectangle class on the stack
// Main is the window the the calculations of the program
int main()
{
const int WIDTH = 4, HEIGHT = 7;
rectangle(WIDTH, HEIGHT);//use the paramaterized constructor to populate the object.
cout << "\nThe size of the retangle is\t" << rectangle.getWidth() << "by\t" << rectangle.getHeight() << endl;
cout << "\nThe area of the retangle is\t" << rectangle.getArea();
system("PAUSE");
return 0;
}
|
MyRectangle.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 "MyRectangle.h"
MyRectangle::MyRectangle()
{
width = 0, height = 0;
}
MyRectangle::MyRectangle(int a,int b)
{
width = a, height = b;
}
int MyRectangle::getHeight(void)
{
return height;
}
int MyRectangle::getWidth(void)
{
return width;
}
int MyRectangle::getArea(void)
{
return width*height;
}
|
Driver.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include "MyRectangle.h" //we #include the header file for the MyRectangle Class
#include <iostream>
#include<string>
using namespace std;
MyRectangle rectangle(int,int); //make an instance of the myRectangle class on the stack
int main()// Main is the window the the calculations of the program
{
const int WIDTH = 4, HEIGHT = 7;
rectangle(WIDTH, HEIGHT);//use the paramaterized constructor to populate the object.
cout << "\nThe size of the retangle is\t" << rectangle.getWidth() << "by\t" << rectangle.getHeight() << endl;
cout << "\nThe area of the retangle is\t" << rectangle.getArea();
system("PAUSE");
return 0;
}
|
It won't compile. I have squiggly red lines of death underneath "rectangle" when I'm calling the functions in my cout lines: rectangle.getWidth(), rectangle.getHeight(), and rectangle.getArea().
The error says they must have a class type if I hover my mouse cursor over them and in the error list this is stated:
Error 2 error C2228: left of '.getWidth' must have class/struct/union