I have been tasked to calculate the area and perimeter of a rectangle using class. The input function is supposed to be inside class. I wrote code so far but there seem to be errors I can't detect.
Some help would be appreciated.
rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle{
public:
int width, length;
Rectangle();
Rectangle(int width1,int length1);
void getRectangle();
int getArea();
int getPerimeter();
};
#endif // RECTANGLE_H
rectangle.cpp
// oporer ta class
#include<iostream>
#include "rectangle.h"
Rectangle::Rectangle()
{
width=0;
length=0;
}
Rectangle::Rectangle(int width1,int length1)
{
width=width1;
length=length1;
}
void Rectangle::getRectangle()
{
cin>>width>>length;
}
int Rectangle::getArea()
{
return (width*length);
}
int Rectangle::getPerimeter()
{
return (width+length)*2
}
// oporer ta rectangle cpp
main.cpp
#include <iostream>
#include "rectangle.h"
using namespace std;
int main()
{
Rectangle paraFirst();
paraFirst.getRectangle();
return 0;
}
// main fucntion
Assuming that the #includes work in your local setup, I see two typos here:
cin>>width>>length; must std::cin >> width >> length;
missing semicolon after return (width+length)*2
And probably the main problem:
Rectangle paraFirst();
is parsed as a declaration for a function that takes no arguments and returns a Rectangle. See also Most vexing parsing. To call the default constructor simply use
Rectangle paraFirst;
or
Rectangle paraFirst{};
Related
Im trying to seperate a class and its superclass into two different header and cpp files. In the main Method I want to include both of them.
Currently my main programm example.cpp looks like this:
#include <iostream>
#include "header.h"
using namespace std;
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
My Rectangle.cpp like this:
#include <iostream>
#include "Shape.h"
#include "Rectangle.h"
using namespace std;
int getArea()
{
return (Shape::width * Shape::height);
}
And my Rectangle.h:
class Rectangle: public Shape
{
public:
int getArea();
};
The Shape.cpp:
#include <iostream>
#include "Shape.h"
using namespace std;
void Shape::setWidth(int w)
{
width = w;
}
void Shape::setHeight(int h)
{
height = h;
}
And the Shape.h:
class Shape
{
public:
void setWidth(int w);
void setHeight(int h);
int width;
int height;
};
The header.h just includes the two headers of the classes:
#include "Shape.h"
#include "Rectangle.h"
If I compile it, the compiler says:
Lukass-MacBook-Pro:Oberklassenbeispiel Lukas$ g++ -c Rectangle.cpp
Rectangle.cpp:9:17: error: invalid use of non-static data member 'width'
return (Shape::width * Shape::height);
~~~~~~~^~~~~
Rectangle.cpp:9:32: error: invalid use of non-static data member 'height'
return (Shape::width * Shape::height);
~~~~~~~^~~~~~
2 errors generated.
It seems, that the Rectanlge.cpp cant see the attributes of the superclass. How do I fix this?
When you #include, you provide the file name, not the classes specified in the file. It is quite common to have multiple closely coupled classes in a single file. Your example isn't really the best (it is single inheritance, not multiple as the title suggests), but working with that...
You could have a file, shapes.h with the following contents :
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
Then in your main function (and the file containing it).
#include <iostream>
#include "shapes.h"
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
I want to know how to use members from other files in main c++ code file?
I already know that we can put our declarations in header files.
Let me summarize my current knowledge and missing parts:
Example 1 - using a function from another file
other.cpp
int Add(int a,int b)
{
return a+b;
}
main.cpp
int Add(int,int); //the most important part
int main()
{
Add(12,2);
}
Example 2 - using a global variable from another file
other.cpp
int something=12;
main.cpp
extern int something; //the most important part
int main()
{
cout << something;
}
Question 1 - create an object from a class in another file???
other.cpp
class Rectangle
{
public:
int width;
int height;
int area() { return height*width }
};
main.cpp
int main()
{
Rectangle a; //gives me error,what should i do?
}
You should start using header files. Typically you would have
Rectangle.h a header file with the declarations
class Rectangle{
public:
Rectangle();
Rectangle(int x, int y);
int width;
int height;
int area();
};
Rectangle.cpp Then you'd have a corresponding cpp file with the definitions
#include "Rectangle.h"
Rectangle::Rectangle()
{
width = 0;
height = 0;
}
Rectangle::Rectangle(int x, int y)
{
width = x;
height = y;
}
Rectangle::area()
{
return height*width;
}
Now in your main.cpp
#include "Rectangle.h"
int main()
{
Rectangle a;
}
You need to declare a class Rectangle in main.cpp, preferably using a header file, ex:
other.h
class Rectangle
{ public:int width; int height; int area(); };
other.cpp
#include other.h
int Rectangle::area() { return height*width; }
main.cpp
#include "other.h"
int main()
{
Rectangle a; //gives me error,what should i do?
}
Following your pattern, it should be simply class Rectangle; (forward declaration).
However, you really should consider to create a Rectangle.h and Rectangle.[C|cpp|cc] (any of them), and include the header instead of forward declaring.
The header should contain the declaration, the source the definition.
Place the declaration of the class in a header file. The definition is usually placed in a source file which uses #include to reference the declaration header. Any other source file which require usage of the class will then simply #include the class declaration header.
So I'm a begging in C++, Im making a program to calculate the area and the perimeter of a triangle when the user inputs the base, height and sides. I have 2 classes, Area and Perimeter, I need to access the variable "base" from Area and use them in Perimeter, since I need the base to calculate perimeter. How can I do this? Also would it be more effective to use one class for this program?
Area.h
#ifndef AREA_H
#define AREA_H
#include <iostream>
using namespace std;
class Area
{
private:
int height;
public:
int base;
Area();
int calcArea();
};
#endif // AREA_H
Area.cpp
#include "Area.h"
#include <iostream>
using namespace std;
Area::Area()
{
cin >> base;
cin >> height;
};
int Area::calcArea(){
int answer;
answer = (base * height)/2;
return answer;
}
Perimeter.h
#ifndef PERIMETER_H
#define PERIMETER_H
#include "Area.h"
#include <iostream>
using namespace std;
class Perimeter
{
private:
int s1;
int s2;
public:
Perimeter();
int calcP();
};
#endif // PERIMETER_H
Perimeter.cpp
#include "Perimeter.h"
#include "Area.h"
#include <iostream>
using namespace std;
Perimeter::Perimeter()
{
cin >> s1;
cin >> s2;
}
int Perimeter:: calcP(){
int answer;
answer = s1 + s2 + base ;
return answer;
}
Instead of creating two classes, Create a class Triangle.
class Triangle
{
private:
int base;
int height;
public:
Triangle();
int CalculatePerimeter();
int CalculateArea();
};
Define the functions.
Create objects of Triangle and call the functions.
Im new in cpp, and tried to google the problame with no luck solving it, could use help.
Oh, and my files are all in the same folder.
the Rectangle.h file:
#ifndef RECTANGLE_H_
#define RECTANGLE_H_
#include "Polygon.h"
class Rectangle: public Polygon{
public:
Rectangle(int nColor);
Rectangle(Rectangle& r);
virtual string getType();
int getArea();
};
#endif /* RECTANGLE_H_ */
The errors are here, under each method signature it sais:
"first defined here"
the Rectangle.cpp file:
#include "Rectangle.h"
#include "Polygon.h"
//error is - "first defined here"
Rectangle::Rectangle(int color):Polygon(color, 3, "Rectangle"){
}
//error is - "first defined here"
Rectangle::Rectangle(Rectangle& r) :
Polygon(r) {
}
//error is - "first defined here"
string Rectangle::getType() {
return "Rectangle";
}
//error is - "first defined here"
int Rectangle::getArea() {
return 0;
}
I am also adding the father class "Polygon.h" but im pretty sure it's not relevant...
the only interesting thing about it is the method:
virtual string getType();
which is not abstract. just virtual
Polygon h:
#ifndef __POLYGON_H
#define __POLYGON_H
#include "Point.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Polygon {
public:
Polygon(int nColor , int nPoints , std::string type);
Polygon(const Polygon &polyOther);
virtual ~Polygon() = 0;
int getColor();
virtual string getType();
void addPoint(Point* p);
Point* getPoint(int index) const;
int getNumOfPoints() const;
Polygon& operator=(const Polygon &polyOther);
private:
std::vector<Point*> _points;
const int _color;
string _type;
};
#endif
thanks for the help....
There is one header a file Rectangle.hxx
#ifndef Rectangle_included
#define Rectangle_included
#include <iostream>
#include <math.h>
#include "GetL.hxx"
using namespace std;
class Rectangle: public GetL
{
int width;
int value;
public:
Rectangle();
Rectangle(int v, int w);
Rectangle(const Rectangle& b);
int getWidth();
int getValue();
Rectangle & plus(int newval);
};
#endif //Rectangle_included
The file GetL.hxx is defined like this:
#ifndef GetL_included
#define GetL_included
#include <iostream>
using namespace std;
class GetL
{
public:
virtual int getWidth();
};
#endif //GetL_include
The file Rectangle.cxx contains various definitions:
#include <iostream>
#include <math.h>
#include "Rectangle.hxx"
using namespace std;
Rectangle::Rectangle()
{
value=0;
width=0;
}
Rectangle::Rectangle(int v, int w)
{
value=v;
width=w;
}
Rectangle::Rectangle(const Rectangle& b)
{
value= b.value;
width= b.width;
}
int Rectangle::getWidth()
{
return width;
}
int Rectangle::getValue()
{
return value;
}
Rectangle& Rectangle::plus(int newval)
{
value+=newval;
if(value>=pow(2,width))
cout<<"Overflow";
return *this;
}
But i am getting the error on compiling Rectangle.cxx.
/tmp/cclETn3R.o:Rectangle.cxx:(.text$_ZN4GetLC2Ev[GetL::GetL()]+0*8): undefined reference to 'vtable for Getl'
How can i remove it? How can i define file GetL.cxx or i don't need to?
You need to compile the different files without linking first. On UNIX compilers this is typically done using the -c option. When building the executable you then specify all the produced .o objects. Alternatively you can specify all source files at once but this is really only viable for very small projects.
You must implement GetL::getWidth(). Given your other files, you will probably implement it in GetL.cxx.