I searched through some of the other pages with this same error, but my code does not have any of their issues that I can find. It could just be that I am tired and slightly new to C++, if so sorry.
I have a parent class, shape.h, that has three derived classes; rectangle.h, triangle.h, and circle.h. The parent class is the one that gets the error "Redefinition of 'Shape'" on the third line. For the life of me I can not figure out what is wrong, other than possible the way I call the shape constructors from the derived classes. Please help, anymore information you need just let me know.
Shape.h:
#include < cmath>
class Shape
{
public:
//constructors
Shape();
Shape(float a);
Shape(float a, float b);
//Returns
float area();
float perimeter();
protected:
float base;
float height;
float radius;
};
Shape.cpp:
#include "Shape.h"
//constructors
Shape::Shape()
{
base = 0;
height = 0;
radius = 0;
}
Shape::Shape(float a)
{
radius = a;
}
Shape::Shape(float a, float b)
{
base = a;
height = b;
}
//returns
float Shape::area()
{
return 0;
}
float Shape::perimeter()
{
return 0;
}
All derived classes are the same except different calculations , so here is Circle.h:
#include "Shape.h"
class Circle: public Shape
{
public:
//constructors
Circle();
Circle(float a);
//Returns
float area();
float perimeter();
private:
};
Circle.cpp:
#include "Circle.h"
//constructors
Circle::Circle():Shape()
{
}
Circle::Circle(float a):Shape(a)
{
}
//returns
float Circle::area()
{
return (3.14 * pow(radius,2));
}
float Circle::perimeter()
{
return (2 * 3.14 * radius);
}
In Circle.cpp Shape.h indirectly included twice and causes mentioned compilation error.
Add include guard to your headers. E.g. Shape.h should be:
#ifndef SHAPE_H
#define SHAPE_H
// Put your Shape class here
#endif
The other approach is to use #pragma once at the beggining of Shape.h if your compiler supports it.
Related
They need to be classes and get area needs to be part of shape, the method getArea needs to be in shape and area needs to be protected and in shape, widght height and radius part of their respective subclass (C++)
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <math.h>
using namespace std;
class shape {
protected:
double area;
public:
double getArea(){return area;};
};
class rectangle:shape {
private:
double width;
double height;
public:
rectangle(){
width=3;
height=4;
}
double getHeight(){return height;};
double getWidth(){return width;};
void setHeight(double h){height=h;};
void setWidth(double w){width=w;};
void setArea(double width, double height){area=height*width;};
};
class circle:shape {
private:
double radius;
public:
circle(){
radius=1;
}
double getRadius(){return radius;};
void setRadius(double r){radius=r;};
void setArea(double width, double height){area=M_PI*(pow(radius,2));};
};
int main () {
rectangle miRectangulo;
circle miCirculo;
cout<<"Area of the rectangle is "<<miRectangulo.getArea()<<endl;
cout<<"Area of the circle is "<<miCirculo.getArea();
return 0;
}
They do need to follow these certain conditions, right now I am getting the error that "double::shape getArea() is inaccesible"
By default, inheritance is private, meaning getArea isn't going to be accessible. Inherit as a public base instead.
class circle: public shape
{
/* snip */
};
ok so i've just encountered some weird problem.
i have the following code:
#pragma once
#include "Point.h"
#include "Shape.h"
class Polygon : public Shape{
public:
Polygon(int = 0);
Point& operator[] (int) const;
protected:
Point *Points;
private:
int Num_Of_Points;
};
Cpp file
Polygon::Polygon(int Num) : Num_Of_Points(Num) { Points = new Point[Num_Of_Points](); }
Point& Polygon::operator[] (int index) const{ return Points[index]; }
2nd header.
#pragma once
#include "Polygon.h"
class Triangle : public Polygon{
Triangle(Point, Point, Point);
private:
double Area;
};
Triangle::Triangle(Point A, Point B, Point C) : Polygon (3) {
Points[0] = A;
Points[1] = B;
Points[2] = C;
}
now i get the following error at Triangle constructor:
Error 1 error C2248: 'Polygon::Points' : cannot access private member declared in class 'Polygon'
i cant understand why, Points is protected member and not private (i get the same error when i put it under public aswell)...
help would be much appriciated!
EDIT: forgot to mention, polygon is abstract class, it inherit from shape some abstract functions.
studying for a final and decided to build a program which makes use of pure virtual functions and polymorphism. i am stuck on a really weird error maybe i am missing something.
This is the Shape abstract class
#ifndef Shape_hpp
#define Shape_hpp
#include <stdio.h>
#include <string.h>
class Shape{
const char* name;
public:
Shape(const char* abc);
virtual double getPerimeter()=0;
virtual double getArea()=0;
};
#endif /* Shape_hpp */
The Shape .cpp implementation file
#include "Shape.hpp"
Shape::Shape(const char *shape){
name = shape;
}
The Circle Header file
#ifndef Circle_hpp
#define Circle_hpp
#include "Shape.hpp"
#include <stdio.h>
class Circle:public Shape{
double m_radius;
public:
Circle(double rad);
double getRadius();
};
#endif /* Circle_hpp */
The circle .cpp implementation file
#include "Circle.hpp"
#include "Shape.hpp"
Circle::Circle(double rad):Shape("Circle"){
m_radius = rad;
}
double Circle::getRadius(){
return m_radius;
}
double Circle::getPerimeter(){
return (2 * 3.14 * m_radius);
}
double getArea(){
return 0;
}
I declared the two pure virtual functions in the abstract "shape" class and am accessing the public of shape class in circle header file, if i declare the pure virtual functions in the circle class it will make it abstract... the error says "Out-of-line definition of 'getPerimeter' does not match any declaration in 'Circle'"
Am i missing something or am i thinking about this the wrong way..
Help would be appreciated. Thanks!
You need to declare all member functions that you define. So in class Circle you need to add:
virtual double getPerimeter();
Or better in C++11:
double getPerimeter() override;
You're defining Circle::getPerimeter() in your .cpp file but there is no member function getPerimeter() in the Circle class declaration. All pure virtual functions need to be overriden in a derived class in order for the class to become concrete. So yes, virtual double getPerimeter(); and override if you're using C++11.
Also, it's good practice to declare simple getters const.
It should be done this way.
class Shape{
const char* name;
public:
Shape(const char* abc);
virtual ~Shape() {} // you should have virtual destructor here
virtual double getPerimeter()=0;
virtual double getArea()=0;
};
class Circle:public Shape{
double m_radius;
public:
Circle(double rad);
double getRadius();
virtual double getPerimeter(); // we need to re-declare it here
virtual double getArea(); // we need to re-declare it here
};
Here's a suggestion. Since Shape is an abstract class, we cannot create objects of the class; so get rid of its constructor. Since we are interested in area and parameter of shapes, define the functions as virtual.
So, here is a redeclaration of Shape class.
#ifndef __SHAPE__
#define __SHAPE__
namespace shape
{
class Shape
{
public:
virtual float getArea()=0;
virtual float getPerimeter()=0;
};
}
#endif
Now, redeclaration of Circle class
#ifndef __CIRCLE__
#define __CIRCLE__
#include "inc/Shape.hpp"
namespace shape
{
class Circle: public Shape
{
float radius;
public:
Circle(float=0.0);
float getArea();
float getPerimeter();
};
}
#endif
Now redefining Circle class
#include "inc/Circle.hpp"
namespace shape
{
Circle::Circle(float radius)
{
this->radius = radius;
}
float Circle::getArea()
{
return ((22/7) * (this->radius * this->radius));
}
float Circle::getPerimeter()
{
return (2 * (22/7) * this->radius);
}
}
Now, in the main class
#include <iostream>
#include "inc/Circle.hpp"
int main()
{
shape::Shape *circle = new shape::Circle(2.5);
std::cout << "Area: " << circle->getArea() << std::endl;
std::cout << "Perimeter: " << circle->getPerimeter() << std::endl;
return 0;
}
You may redeclare the classes without namespaces.
The point to note is that the type of object created should be of the parent class and the object itself should be a child class.
One last thing; all pure virtual functions in the abstract must be redeclared and redefined (overridden) in the derived classes.
Here is my task:
Design classes Circle and Square which are inherited from class Shape (which contains center of gravity which is common characteristic for all shapes, function for moving center of gravity for specific value and virtual functions circumference, area and read). Classes should have specific functions for calculating circumference and area, as well as for reading data members.
Here is what I have done:
#include <iostream>
using namespace std;
class Point {
private:
float x;
float y;
public:
Point();
Point(float,float);
~Point();
};
Point::Point() {
}
Point::Point(float a,float b) {
x=a;
y=b;
}
Point::~Point() {
}
class Shape {
public:
Shape(void);
Shape(Point);
virtual float circumference(void) {}
virtual float area(void) {}
protected:
Point center_of_gravity;
};
Shape::Shape(void) {
}
Shape::Shape(Point a) {
center_of_gravity=a;
}
//----------------------------------------
class Circle:public Shape {
private:
float radius;
public:
float x;
float y;
Circle();
Circle(float);
virtual float circumference(void);
virtual float area(void);
};
Circle::Circle(void) {
}
Circle::Circle(float a) {
radius=a;
}
float Circle::area(void) {
float area_of_circle;
const float pi=3.14159;
area_of_circle=radius*radius*pi;
return area_of_circle;
}
float Circle::circumference(void) {
float circumference_of_circle;
const float pi=3.14159;
circumference_of_circle=2*radius*pi;
return circumference_of_circle;
}
//----------------------------------------
class Square:public Shape {
private:
float length;
public:
Square();
Square(float);
virtual float circumference(void);
virtual float area(void);
};
Square::Square(void) {
}
Square::Square(float a) {
length=a;
}
float Square::area(void) {
float area_of_circle;
area_of_circle=length*length;
return area_of_circle;
}
float Square::circumference(void) {
float circumference_of_square;
circumference_of_square=4*length;
return circumference_of_square;
}
int main() {
float a,b;
cout<<"Enter coordinates of center of gravity: "<<endl;
cin>>a>>b;
Point center_of_grav(a,b);
cout<<"Enter length of square: "<<endl;
cin>>a;
Square square(a);
cout<<"Enter radius of circle: "<<endl;
cin>>a;
Circle circle(a);
Shape *shape1=&circle;
Shape *shape2=□
cout<<"Area of circle is "<<shape1->area()<<", circumference is "<<shape1->circumference()<<endl;
cout<<"Area of square is "<<shape2->area()<<", circumference is "<<shape2->circumference()<<endl;
}
Is it ok? How can I realize function for moving center of gravity?
How can I read center of gravity for Circle and Square (it is inherited from class Shape)? They mean it (I think) when they say "...as well as for reading data members."
EDIT:
After I put all suggestion I got in one place:
#include <iostream>
using namespace std;
class Point{
private:
float x;
float y;
public:
Point();
Point(float,float);
~Point();
friend class Shape;
};
Point::Point(){
}
Point::Point(float a,float b){
x=a;
y=b;
}
Point::~Point(){
}
//----------------------------------------
class Shape{
public:
Shape(void);
Shape(Point);
virtual float area(void)=0;
virtual float circumference(void)=0;
protected:
float x_coordinate;
float y_coordinate;
Point center_of_gravity;
};
Shape::Shape(void){
}
Shape::Shape(Point a){
center_of_gravity=a;
x_coordinate=a.x;
y_coordinate=a.y;
}
//----------------------------------------
class Circle:public Shape{
private:
float radius;
public:
Circle();
Circle(Point,float);
virtual float area(void);
virtual float circumference(void);
};
Circle::Circle(void){
radius=0;
}
Circle::Circle(Point p,float a) : Shape(p), radius(a){
}
float Circle::area(void){
float area_of_circle;
const float pi=3.14159;
area_of_circle=radius*radius*pi;
return area_of_circle;
}
float Circle::circumference(void){
float circumference_of_circle;
const float pi=3.14159;
circumference_of_circle=2*radius*pi;
return circumference_of_circle;
}
//----------------------------------------
class Square:public Shape{
private:
float length;
public:
Square();
Square(Point,float);
virtual float area(void);
virtual float circumference(void);
};
Square::Square(void){
length=0;
}
Square::Square(Point p,float a) : Shape(p), length(a){
}
float Square::area(void){
float area_of_circle;
area_of_circle=length*length;
return area_of_circle;
}
float Square::circumference(void){
float circumference_of_square;
circumference_of_square=4*length;
return circumference_of_square;
}
//----------------------------------------
int main(){
float a,b;
cout<<"Enter coordinates of center of gravity: "<<endl;
cin>>a>>b;
Point center_of_grav(a,b);
cout<<"Enter length of square: "<<endl;
cin>>a;
Square square(center_of_grav,a);
cout<<"Enter radius of circle: "<<endl;
cin>>a;
Circle circle(center_of_grav,a);
Shape *shape1=&circle;
Shape *shape2=□
cout<<"Area of circle is "<<shape1->area()<<", circumference is "<<shape1->circumference()<<endl;
cout<<"Area of square is "<<shape2->area()<<", circumference is "<<shape2->circumference()<<endl;
}
1.Constructor design
How will you set their gravity center ?
Point center_of_grav(a,b);
...
Square square(c); // what's the gravity center ?
You have to design the constructor of the derived, so that it has all information required to construct the base:
Square square(center_of_grav, c);
To achieve this, you have to define it in the following way (of course adapt the class definition accordingly):
Square::Square(Point p, float a) : Shape(p), lentgh(a) {
// ... reserve this for more complex initisalisations
}
Note that your default constructor leave the objects uninitialized.
2.Design of Shape
Very important: Shape is a polymorphic class with virtual functions. You shall take the habit to define a virtual destructor in this case.
Minor remark: It makes no sense to create directely a Shape object. It's an abstract concept. There is no default rule to calculate an area or a circumference that could apply to most of the shape. Therefore, I'd strongly suggest to define these two functions as pure virtual:
class Shape {
...
virtual float circumference(void) =0; // pure virtual
virtual float area(void) = 0;
...
};
Advantage: as soon as you have a pure virtual function in a class, this class becomes abstract, and you won't be able to instantiate an object of that class by error. The compiler will make sure that you instatiate only concrete derivates of the abstract concept.
3.Make sure your code complies with requirements
Well it's a detail and you certainly have taken care of it, but:
Classes should have specific functions for calculating circumference
and area, as well as for reading data members.
So I think you should foresee some getters to access to the protected data:
Example:
class Shape {
...
Point get_gravity_center();
...
};
Point Shape::get_gravity_center() {
return center_of_gravity;
}
I let you complete the others. Simply imagine that you have to print in main() the description of a circle (coordinates of the center and radius), and you'll see the getters that are missing.
Edit following your question:
As you we've defined the getter for the center of gravity at the shape level, you don't need to define it again in the derivates. You could then simply call it in main(). The problem is that you also need to access to the coordinats of that point. So:
class Point {
...
float get_x() { return x; }
float get_y() { return y; }
...
};
With this you can write in main():
cout << "Circle of center ("<<circle.get_gravity_center().get_x()<<","
<< circle.get_gravity_center().get_y()<<")"<<endl;
P.S: with the center_of_gravity as member of shape, you no longer need to duplicate the coordinates otf that point.
To answer your question, it is not OK. Your code does show some pretty clear thinking, so that's good. It just needs to be finished. All data members of all classes must be given values when objects are constructed. Otherwise, they get random values, and that is never good practice. So init the CG in the Shape constructor.
Also, remove x and y from circle, if you mean them to represent CG -- that info is inherited already.
Finally, to realize a function for moving CG, I'd recommend adding a setter method to the Shape class, where it can be inherited by those other two classes. With that, you should be good to go.
I'm trying to inherit from one of my class 2 variables which they must be equal to be able to return the value of one of my functions in the other class..
class Rectangle: public Shape{
double Length;
double Width;
public:
Rectangle(double Length, double Width):
Shape("Rectangle")
{
this->Length=Length;
this->Width=Width;
}
double getPerimerter(){
return 2 * (Length+Width);
}
double getArea(){
return Length * Width;
}
};
class Square: public Shape, public Rectangle{
double Side;
public:
Square():
Shape("Square"),
Rectangle(Length,Width)
{}
double getPerimerter(){
if(Length==Width)
return 4 * (Length+Width);
}
double getArea(){
if(Length==Width)
return (Length+Width) * (Length+Width);
}
};
as you can see I have already a concrete class call Rectangle which hold to private variables with the names Length and Width.. What I'm trying to do is inherit this class to my class Square and if Length and Width are equal then I can return the Area and Perimeter of the Square..
It is perfectly fine to inherit from an existing class.
What you probably want, however, is this:
class Square: public Rectangle
{
public:
Square(double Side) : Rectangle(Side, Side) { }
};
That way, there is no problem of someone trying to use a ractangular square Square(4.3, 9.6).
Alternatively, you could of course use typedef Rectangle Square;
Edit:
To overcome the "name", we could do something like this:
class Rectangle
{
public:
Rectangle(double Length, double Width) : Shape("Rectangle") { ... }
protected:
Rectangle(double Length, double Width, const char *name) : Shape(name), Length(Length), Width(Width) {}
};
class Square
{
public:
Square(double side) : Rectangle(side, side, "Square") {}
};
Edit2: Code that I came up with:
#include <iostream>
using namespace std;
class Shape
{
private:
const char *name;
public:
Shape(const char *name) : name(name) {}
virtual double getPerimeter() = 0;
virtual double getArea() = 0;
};
class Rectangle: public Shape{
double Length;
double Width;
public:
Rectangle(double Length, double Width):
Shape("Rectangle")
{
this->Length=Length;
this->Width=Width;
}
double getPerimeter(){
return 2 * (Length+Width);
}
double getArea(){
return Length * Width;
}
protected:
Rectangle(double Length, double Width, const char *name):
Shape(name)
{
this->Length=Length;
this->Width=Width;
}
};
class Square: public Rectangle
{
public:
Square(double Side):
Rectangle(Side,Side, "Square")
{
}
};
int main()
{
Square sq(10.0);
Rectangle rect(12.0, 4.0);
cout << "sq:" << sq.getArea() << " rect:" << rect.getArea() << endl;
}
As a first thing you should add an argument to Square constructor, like so:
Square(double Side):Rectangle(Side,Side){
// Add additional constructor code here if required
}
About returning a value in a constructor - that is a no-go as the constructor might not return anything.
I hope this is what you meant as your question was quite hard for me to understand.
I might not understand your question, but it looks to me like you only need to add a parameter to your Square() constructor. It would look like this:
class Square: public Rectangle{
public:
Square(double dimension):
Rectangle(dimension, dimension){
}
};
There is no need for a conditional statement in the Square() constructor. Creating a Square necessarily implies creating a Rectangle with equal length and width.