How to properly use a class object (Point) inside my class (Segment) object that they both inheritoring from the class(Figure2D)?
The Figure.h file:
#include <math.h>
#include <ostream>
class Figure2D
{
private:
double x;
double y;
double length;
double height;
char* name = NULL;
public:
Figure2D(double l, double h, double x = 0, double y = 0);
Figure2D(Figure2D& f);
Figure2D();
virtual ~Figure2D();
const Figure2D& operator=(Figure2D& d);
void setName(const char*);
double getX()const;
double getY()const;
double getLength()const;
double getHeight()const;
const char* getName()const;
double Area()const;
double Perimeter()const;
void Shift(double dx, double dy);
void MoveTo(double newX, double newY);
void Resize(double newL, double newH);
void Scale(double Kx, double Ky);
bool isInside(Figure2D* P);//Point(P->getX(),P->getT()) lies inside the figure
void print()const;
friend std::ostream& operator<<(std::ostream& o, Figure2D& d);
};
The Point.h file:
#pragma once
#include "Figure2D.h"
class Point : public Figure2D {
public:
Point(const char* s,int x,int y) :Figure2D(0,0,x,y)
{
setName(s);
}
Point(){}
Point(const Point& other)
{
}
~Point() {}
};
The Segment.h file:
#pragma once
#include "Figure2D.h"
#include "Point.h"
class Segment : public Figure2D
{
public:
Segment(const char* s, Point& p1, Point& p2)
{
setName(s);
if (p1.getX() <= p2.getX() && p1.getY() <= p2.getY())
{
isLeftToRightSegment = true;
}
else
{
isLeftToRightSegment = false;
}
}
protected:
bool isLeftToRightSegment;
};
I cant make this code in the main.cpp properly:
Segment S("Segment PQ", P, Q);
Eventually i want to make shaps as(Point,Segment,Rectangle,Square,Circle) with the
Figure2D class
Related
class Point2d
{
public:
Point2d();
Point2d(int X, int Y);
~Point2d();
int m_x;
int m_y;
};
class Creature
{
private:
Point2d& m_yer;
const std::string& m_Name;
public:
Creature() = delete;
Creature(const std::string& name, Point2d& location) : m_Name(name), m_yer(location) {}
void Creature_Move(int x, int y);
~Creature();
};
int main()
{
std::string name = "Monke";
Point2d abc{ 25,30 };
Creature canavva(name, abc);
//Creature canavv(name, Point2d{ 40,50 }); //Gives error
return 0;
}
But when I put const in the argument Creature(const std::string& name, const Point2d& location) and const Point2d& m_yer; then
Creature canavv(name, Point2d{ 40,50 }) doesn't give error.
How can I initiliaze Poin2d without const keyword because I'm changing values inside it.
I would like to check wether my 2d point and 3d points are identical using operator== that returns true if they are.
How can I implement that?
Do I need to add overloaded operator into both classes or what?
How to compare 2 arguments x and y?
My code:
#include <iostream>
using namespace std;
class Point2D {
public:
Point2D();
// ~Point2D();
void SetX(double x);
void SetY(double y);
double GetX();
double GetY();
protected:
double m_x, m_y;
};
class Point3D :Point2D {
public:
Point3D() :Point2D() { m_z = 0.0; };
protected:
double m_z;
};
Point2D::Point2D() {
m_x = 0.0;
m_y = 0.0;
}
void Point2D::SetX(double x) {
m_x = x;
}
void Point2D::SetY(double y) {
m_y = y;
}
double Point2D::GetX() {
return m_x;
}
double Point2D::GetY() {
return m_y;
}
int main() {
Point2D T;
cout << T.GetX() << " " << T.GetX() << endl;
return 0;
}
First, you probably want public inheritance:
class Point3D : public Point2D {
// ~~~~~~~~~~~~~^
Once you have that, you can rely on polymorphism to handle things for you and only implement comparison operator for Point2D:
// the arguments should be const, but your classes are not const correct
bool operator== (/*const*/ Point2D& lhs, /*const*/ Point2D& rhs)
{
return lhs.GetX() == rhs.GetX() && lhs.GetY() == rhs.GetY();
}
int main() {
Point2D p2;
Point3D p3;
std::cout << std::boolalpha << (p2 == p3);
}
You can further improve it by making your functions const:
class Point2D {
public:
Point2D();
void SetX(double x);
void SetY(double y);
double GetX() const; //here
double GetY() const; //here
protected:
double m_x, m_y;
};
double Point2D::GetX() const { //and here
return m_x;
}
double Point2D::GetY() const { //and here
return m_y;
}
//now you can pass arguments by const reference, no accidental modifications in the operator
bool operator== (const Point2D& lhs, const Point2D& rhs)
{
return lhs.GetX() == rhs.GetX() && lhs.GetY() == rhs.GetY();
}
I am trying to initialize an object of a class inside a member function of another class. The problem is that I need to pass a function pointer to the constructor. I do not know how I can make this. This is the error:
no matching function for call to ‘inheritance01::inheritance01(double (inheritance02::*&)(double))’
inheritance01 LT (func);
The code below shows the problem.
class Base01 {
public:
Base01(double (*)(double));
virtual double calc(double) = 0;
double (*ptr_fd() const)(double) { return ptr_fd_; }
private:
double (*ptr_fd_)(double);
};
Base01::Base01(double (*f)(double))
: ptr_fd_(f)
{
}
//----------------------------------------------------
class inheritance01 : public Base01 {
public:
inheritance01(double (*ptr_f)(double));
virtual double calc(double);
};
inheritance01::inheritance01(double (*pf)(double))
: Base01(pf)
{
}
double inheritance01::calc(double t) { return 2.0 * t; }
//###################################################
class Base02 {
public:
Base02(double);
virtual double solution(double, double) = 0;
double a() { return a_; };
private:
const double a_;
};
Base02::Base02(double aa)
: a_(aa)
{
}
//------------------------------------------------------
class inheritance02 : public Base02 {
public:
inheritance02(double, double);
virtual double solution(double, double);
//static double sol_aux (double);
private:
double sol_aux(double);
const double b;
//double (inheritance02::*fptrsol_aux)(double u) = &inheritance02::sol_aux;
typedef double (inheritance02::*fptr)(double u);
fptr func;
};
inheritance02::inheritance02(double aa, double bb)
: Base02(aa)
, b(bb)
{
//func = double (*sol_aux)(double);
//func = &inheritance02::sol_aux;
}
//--------------------------------------------------
double inheritance02::sol_aux(double u)
{
return (a() + b) / u;
}
//--------------------------------------------------
double inheritance02::solution(double x, double t)
{
//inheritance01 LT (&func);
//inheritance01 LT (this->func);
//inheritance01 LT (&fptrsol_aux);
inheritance01 LT(func); // Here is the problem
return LT.calc(x + t);
}
//########################################################
#include <iostream>
int main()
{
inheritance02 obj(1.0, 1.0);
double value = obj.solution(1.0, 1.0);
std::cout << "value = " << value << std::endl;
return 0;
}
As the comment by #Eljay says, you are creating a typedef for a pointer to member function here:
typedef double (inheritance02::*fptr)(double u);
However, the constructor of inheritance01 takes a regular function pointer as an argument:
inheritance01( double (*ptr_f)(double));
so this line:
inheritance01 LT(func); // Here is the problem
doesn't work because the types don't match up (pointer to member functions are not convertible to function pointers).
The easy fix would be to make func a regular function pointer, like this:
typedef double (*fptr)(double u);
and everything should work fine.
Here's a demo.
Although I am not able to declare the 'func_' parameter as const and private (I do not know how to return a std::function) the following code solves my problem:
#include <iostream>
#include <functional>
using namespace std;
class Base01
{
public:
Base01( std::function<double (double)> );
virtual double calc( double ) = 0;
//function<double (double)> func { return func_; }
function<double (double)> func;
private:
//const function<double (double)> func_;
};
Base01::Base01( function<double (double)> f) : func(f) {}
//----------------------------------------------------
class inheritance01:public Base01
{
public:
inheritance01( function<double (double)> );
virtual double calc( double );
};
inheritance01::inheritance01 (function<double (double)> f): Base01(f){}
double inheritance01::calc(double t) { return Base01::func(2.0*t); }
//###################################################
class Base02
{
public:
Base02(double);
virtual double solution(double, double) = 0;
double a(){return a_;};
private:
const double a_;
};
Base02::Base02(double aa): a_(aa) {}
//------------------------------------------------------
class inheritance02 : public Base02
{
public:
inheritance02( double, double );
virtual double solution(double, double);
private:
double sol_aux (double);
const double b;
};
inheritance02::inheritance02 (double aa, double bb)
: Base02(aa), b(bb)
{}
//--------------------------------------------------
double inheritance02::sol_aux(double u) { return (a()+b)/u; }
//--------------------------------------------------
double inheritance02::solution(double x, double y)
{
inheritance01 LT ( bind( &inheritance02::sol_aux, this, placeholders::_1) );
return LT.calc(x+y);
}
//########################################################
int main()
{
inheritance02 obj (1.0,1.0);
double value = obj.solution(1.0,1.0);
std::cout << "value = " << value << std::endl;
return 0;
}
I wrote a program in c++ that should solve differential equations. The problem is, it seems like it does not work well with ROOT. It compilates fine, but when I execute, this is what I get:
*** Break *** segmentation violation
===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0 0x00007fc28193984a in __GI___waitpid (pid=7730, stat_loc=stat_loc
entry=0x7ffffe4ae000, options=options
entry=0) at ../sysdeps/unix/sysv/linux/waitpid.c:31
#1 0x00007fc2818b2ffb in do_system (line=<optimized out>) at ../sysdeps/posix/system.c:148
#2 0x00007fc2831d0954 in TUnixSystem::StackTrace() () from /usr/lib/root/libCore.so
#3 0x00007fc2831d29ec in TUnixSystem::DispatchSignals(ESignals) () from /usr/lib/root/libCore.so
#4 <signal handler called>
#5 0x0000000000405a8a in Runge_Kutta::Passo(double, VettoreLineare&, double) ()
#6 0x0000000000403b8a in main ()
===========================================================
The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#5 0x0000000000405a8a in Runge_Kutta::Passo(double, VettoreLineare&, double) ()
#6 0x0000000000403b8a in main ()
===========================================================
This is my program
equazione_differenziale.c
#include "equazione_differenziale.h"
EqDifferenzialeBase :: EqDifferenzialeBase (FunzioneBase* f) {
_f=f;
};
Eulero :: Eulero (FunzioneBase*f) : EqDifferenzialeBase(f) {};
Runge_Kutta :: Runge_Kutta (FunzioneBase* f) : EqDifferenzialeBase(f) {};
VettoreLineare Eulero :: Passo (double t, VettoreLineare& x, double h) {
VettoreLineare vec(x.GetN());
vec=x+_f->Eval(t,x)*h;
return vec;
};
Protone :: Protone (double m, double q, double E0, double f, double lambda){
_m=m;
_q=q;
_E0=E0;
_f=f;
_lambda=lambda;
};
VettoreLineare Protone::Eval(double t, const VettoreLineare& v) const{
VettoreLineare y(v.GetN());
for(int i=0; i<v.GetN()/2; i++){
y.SetComponent(i, v.GetComponent(v.GetN()/2+i));
y.SetComponent(i+v.GetN()/2, (-1.)*(_q/_m)*_E0*sin(2*M_PI*(v.GetComponent(i)/_lambda)-2*M_PI*_f*t));
};
return y;
};
VettoreLineare Runge_Kutta::Passo(double t, VettoreLineare& v, double h){
VettoreLineare k1=_f->Eval(t,v);
VettoreLineare k2=_f->Eval(t+h/2.,v+k1*(h/2.));
VettoreLineare k3=_f->Eval(t+h/2.,v+k2*(h/2.));
VettoreLineare k4=_f->Eval(t+h,v+k3*h);
VettoreLineare y=v+(k1+k2*2.+k3*2.+k4)*(h/6.);
return y;
};
equazione_differenziale.h
#ifndef equazione_differenziale_h_
#define equazione_differenziale_h_
#include "Vettore.h"
#include <iostream>
#include <cmath>
class FunzioneBase {
public:
virtual VettoreLineare Eval(double t, const VettoreLineare& v) const=0;
};
class Protone: public FunzioneBase {
private:
double _m,_q,_E0,_f,_lambda;
public:
Protone(double m, double q, double E0, double f, double lambda);
virtual VettoreLineare Eval(double t, const VettoreLineare& v) const;
};
class EqDifferenzialeBase {
protected:
FunzioneBase* _f;
public:
EqDifferenzialeBase (FunzioneBase* f);
virtual VettoreLineare Passo (double t, VettoreLineare& x, double h)=0;
};
class Eulero : public EqDifferenzialeBase {
public:
Eulero (FunzioneBase* f);
virtual VettoreLineare Passo (double t, VettoreLineare& x, double h);
};
class Runge_Kutta: public EqDifferenzialeBase {
protected:
FunzioneBase* _f;
public:
Runge_Kutta (FunzioneBase* f);
virtual VettoreLineare Passo(double t, VettoreLineare& v, double h);
};
#endif
Vettore.h
#ifndef vettore_h_
#define vettore_h_
#include <iostream>
#include <cmath>
#include <fstream>
class Vettore {
protected:
unsigned int _N;
double * _v;
void Quicksort(unsigned int primo, unsigned int ultimo);
void Scambia (int a, int b);
public:
Vettore ();
Vettore (int N);
Vettore (int N, char* nomefile);
Vettore (const Vettore& v);
virtual void SetComponent (int i, double x);
void AddComponent (double x);
double GetComponent (int i) const;
void Print () const;
void Print (char* nomefile) const;
void Sort();
virtual int GetN() const;
Vettore& operator=(const Vettore & vetty);
~Vettore();
};
class VettoreLineare : public Vettore {
protected:
public:
VettoreLineare () : Vettore() {};
VettoreLineare (int N) : Vettore(N) {};
VettoreLineare (int N, char* nomefile) : Vettore(N, nomefile) {};
VettoreLineare (const Vettore& v) : Vettore(v) {};
VettoreLineare operator+(const VettoreLineare& v);
VettoreLineare operator*(double lambda);
VettoreLineare& operator=(const VettoreLineare& v);
virtual int GetN() const;
virtual void SetComponent(int i, double x);
};
Vettore.c
#include "Vettore.h"
//Default Constructor
Vettore :: Vettore () {
_N=0;
_v=NULL;
};
//N Constructor
Vettore :: Vettore (int N) {
_N=N;
_v=new double [_N];
for (int i=0; i<_N; i++)
_v[i]=0;
};
//N file-taken constructor
Vettore :: Vettore (int N, char* nomefile) {
_N=N;
_v=new double [_N];
std::ifstream input;
input.open(nomefile);
double dato;
input>>dato;
for(int i=0; i<N; i++){
_v[i]=dato;
input>>dato;
};
input.close();
};
//Copyconstructor
Vettore :: Vettore (const Vettore& V) {
_N=V.GetN();
_v=new double [_N];
for(int i=0; i<_N; i++)
_v[i]=V.GetComponent(i);
};
//Destructor
Vettore::~Vettore(){
delete[] _v;
};
//Set Component
void Vettore :: SetComponent (int i, double x) {
if (i>_N) {
std::cout<<"errore!"<<std::endl;
return ;
};
_v[i]=x;
};
//Get Component
double Vettore :: GetComponent (int i) const {
if (i>_N){
std::cout<<"errore!"<<std::endl;
return 0;
};
return _v[i];
};
//Add Component (aggiunge il valore desiderato nella coda del vettore)
void Vettore :: AddComponent (double x) {
double* a=new double [_N+1];
for(int i=0; i<_N; i++)
a[i]=_v[i];
a[_N]=x;
delete [] _v;
_v=a;
_N=_N+1;
};
//Print
void Vettore :: Print () const {
std::cout<<"Il vettore ha: "<<_N<<" componenti."<<std::endl;
for(int i=0; i<_N; i++)
std::cout<<_v[i]<<std::endl;
};
//Stampa su file
void Vettore :: Print (char* nomefile) const {
std::ofstream output;
output.open(nomefile);
output<<_N;
for(int i=0; i<_N; i++)
output<<_v[i]<<std::endl;
output.close();
};
//Get _N
int Vettore :: GetN () const {
return _N;
};
//Operatore di Assegnazione
Vettore & Vettore::operator =(const Vettore& vetty){
if (_v) delete [] _v;
_N=vetty.GetN();
_v=new double [_N];
for(int n; n<_N; n++)
_v[n]=vetty._v[n];
return *this;
};
//Algoritmo Quicksort
void Vettore :: Sort (){
Quicksort(0,GetN()-1);
};
void Vettore :: Quicksort (unsigned int primo, unsigned int ultimo) {
if(ultimo-primo<=1){
if (GetComponent(primo)>GetComponent(ultimo)) Scambia(primo, ultimo);
return;
}
double pivot= GetComponent(int((primo+ultimo)/2));
unsigned int basso= primo, alto=ultimo;
while(basso < alto) {
while (GetComponent(basso)<pivot) basso++;
while (GetComponent(alto)>pivot) alto--;
if(basso<alto) { Scambia(basso,alto); basso++;};
};
Quicksort(primo, basso-1);
Quicksort(basso, ultimo);
};
void Vettore :: Scambia(int a, int b){
double k;
k=_v[a];
_v[a]=_v[b];
_v[b]=k;
};
//Operatore somma fra vettori
VettoreLineare VettoreLineare::operator+ (const VettoreLineare& v){
VettoreLineare sum(v.GetN());
for(int i=0; i<_N; i++)
sum.SetComponent(i, _v[i]+v.GetComponent(i));
return sum;
};
//Operatore Moltiplicazione scalare
VettoreLineare VettoreLineare::operator* (double lambda){
for(int i=0; i<_N; i++)
_v[i]=_v[i]*lambda;
return *this;
};
//Operatore Assegnazione
VettoreLineare& VettoreLineare::operator= (const VettoreLineare& k){
if (_v) delete [] _v;
_N=k.GetN();
_v=new double [k.GetN()];
for (int i=0; i<_N; i++)
_v[i]=k.GetComponent(i);
return *this;
};
int VettoreLineare :: GetN() const {
return _N;
};
void VettoreLineare :: SetComponent(int i, double x) {
_v[i]=x;
};
main.c
#include "equazione_differenziale.h"
#include "Vettore.h"
#include "iostream"
#include "TGraph.h"
#include "TApplication.h"
#include "TCanvas.h"
#include "TAxis.h"
using namespace std;
int main () {
//PRIMO PUNTO
//Dichiarazione equazione
Protone* myProt=new Protone (1.67E-27, 1.60E-19, 1.E7, 0.2, 5.E8);
Runge_Kutta myKutta(myProt);
//Dichiarazione DatiIniziali
VettoreLineare DatiIniziali (2);
DatiIniziali.SetComponent(0, 0);
DatiIniziali.SetComponent(1,1E8);
//dichiarazione tempo
double t_min=0;
double h=1E-12;
//definizione variabili root
TApplication myApp("myApp",0,0);
TGraph* g = new TGraph();
//ciclo
for(int i=0;i<(1E-7-t_min)/h;i++){
double x,y;
x=t_min+i*h;
DatiIniziali= myKutta.Passo(x, DatiIniziali ,h);
y=DatiIniziali.GetComponent(0);
g->SetPoint(i,x,y);
};
//Run grafico
TCanvas *c=new TCanvas("C1","C1",1);
c->cd();
g->GetXaxis()->SetTitle("t[s]");
g->GetYaxis()->SetTitle("x[m]");
g->Draw("AL");
myApp.Run();
return 0;
};
The strange thing is that this program works on university computers, but it doesn't on neither of my two computers. I'm thinking this means I installed ROOT badly on both computers, but I sincerely wouldn't know how to proove it.
You declare
class EqDifferenzialeBase {
protected:
FunzioneBase* _f;
and
class Runge_Kutta: public EqDifferenzialeBase {
protected:
FunzioneBase* _f;
and then do
Runge_Kutta :: Runge_Kutta (FunzioneBase* f) : EqDifferenzialeBase(f) {};
and then use it as
VettoreLineare Runge_Kutta::Passo(double t, VettoreLineare& v, double h){
VettoreLineare k1=_f->Eval(t,v);
From these lines it should be pretty clear what goes wrong and where the segmentation violation originates. So get rid of the ghost that shadows the field you really want to use.
And as in the other answer, correct the scalar product of the vector class to the standard of the sum operator.
I am new at C++, and have some problems with it. I would like to create an abstract class, and two more abstract classes that inherit from that class. Finally, those two classes are used to be implemented by concrete classes. Here are my header and source files:
/*
* Cylinder.h
*/
#ifndef CYLINDER_H_
#define CYLINDER_H_
class Shape{
public:
virtual ~Shape();
virtual const double area()=0;
virtual const void printDetails();
};
class Shape2D: virtual public Shape{
public:
virtual const double scope()=0;
};
class Shape3D: virtual public Shape{
public:
virtual const double volume()=0;
};
class Rectangle: public Shape2D{
private:
double a;
double H;
public:
Rectangle();
Rectangle(double a, double H);
Rectangle(Rectangle &r);
double getA() const;
double getH() const;
void setA(double a);
void setH(double H);
};
class Circle: public Shape2D{
private:
double r;
public:
Circle();
Circle(double r);
Circle(Circle &c);
double getR() const;
void setR(double r);
};
class Cylinder: public Shape3D{
private:
Circle base;
Rectangle wrapper;
public:
Cylinder();
Cylinder(Rectangle &r, Circle &c);
Cylinder(double a, double H, double r);
Cylinder(Cylinder &c);
double getHeight() const;
double getBaseRadius() const;
double getBaseArea() const;
double getBaseScope() const;
double getWrapperArea() const;
double getWrapperScope() const;
void setHeight(double H);
void setRadius(double r);
Rectangle* getWrapper() const;
Circle* getBase() const;
};
#endif /* CYLINDER_H_ */
/*
* Cylinder.cpp
*/
#include <iostream>
#include "Cylinder.h"
#include <math.h>
using namespace std;
void Shape2D::printDetails() const{
cout<<"\nArea: "<<area();
cout<<"\nScope: "<<scope();
}
void Shape3D::printDetails() const{
cout<<"\nArea: "<<area();
cout<<"\nVolume: "<<volume();
}
Rectangle::Rectangle(){
H=0.0;
a=0.0;
}
Rectangle::Rectangle(double a, double H){
this->H=H;
this->a=a;
}
Rectangle::Rectangle(Rectangle &r){
a=r.a;
H=r.H;
}
Rectangle::~Rectangle(){
cout<<"Rectangle deallocated.";
}
double Rectangle::getA() const{
return a;
}
double Rectangle::getH() const{
return H;
}
void Rectangle::setA(double a){
this->a=a;
}
void Rectangle::setH(double H){
this->H=H;
}
double Rectangle::scope() const{
return 2*a+2*H;
}
double Rectangle::area() const{
return a*H;
}
void Rectangle::printDetails() const{
cout<<"\nDimensions of rectangle: "<<a<<"*"<<H;
Shape::printDetails();
}
Circle::Circle(){
r=0.0;
}
Circle::Circle(double r){
this->r=r;
}
Circle::Circle(Circle &c){
r=c.r;
}
Circle::~Circle(){
cout<<"Circle deallocated.";
}
double Circle::getR() const{
return r;
}
void Circle::setR(double r){
this->r=r;
}
double Circle::scope() const{
return 2*r*M_PI;
}
double Circle::area() const{
return r*r*M_PI;
}
void Circle::printDetails() const{
cout<<"\nRadius of circle: "<<r;
Shape::printDetails();
}
Cylinder::Cylinder(){
wrapper=new Rectangle(0.0,0.0);
base=new Circle(0.0);
}
Cylinder::Cylinder(Rectangle &r, Circle &c){
base=c;
wrapper=r;
}
Cylinder::Cylinder(double a=0.0, double H=0.0, double r=0.0){
wrapper=new Rectangle(a,H);
base=new Circle(r);
}
Cylinder::Cylinder(Cylinder &c){
c.base=new Circle(c.base);
c.wrapper=new Rectangle(c.wrapper);
}
Cylinder::~Cylinder(){
delete base;
delete wrapper;
}
double Cylinder::getHeight() const{
return wrapper.getH();
}
double Cylinder::getBaseRadius() const{
return base.getR();
}
double Cylinder::getBaseArea() const{
return base.area();
}
double Cylinder::getBaseScope() const{
return base.scope();
}
double Cylinder::getWrapperArea() const{
return wrapper.area();
}
double Cylinder::getWrapperScope() const{
return wrapper.scope();
}
void Cylinder::setHeight(double H){
wrapper.setH(H);
}
void Cylinder::setRadius(double r){
base.setR(r);
wrapper.setA(2*base.getR()*M_PI);
}
double Cylinder::volume() const{
return base*wrapper.getH();
}
double Cylinder::area() const{
return 2*base.area+wrapper.area;
}
void Cylinder::printDetails() const{
cout<<"\nRadius of base of Cilynder: "<<base.getR();
cout<<"\nHeight of wrapper: "<<wrapper.getH();
Shape::printDetails();
}
Rectangle* Cylinder::getWrapper() const{
return new Rectangle(wrapper);
}
Circle* Cylinder::getBase() const{
return new Circle(base);
}
My compiler gives me different errors as I try to modify the code to accomplish somehow to have classes Rectangle, Circle and Cylinder as non abstract ones. I am not sure where should I put method definitions (should they be duplicated in inherited classes), and what is their proper form actually. Sorry for long text. I hope someone can help me. Thanks in advance
Sam
You aren't implementing the pure virtual methods. Whenever you have pure virtual methods (virtual void whatever()=0), that makes the class abstract. Abstract classes can not be instantiated. In order to be instantiated, concrete subclasses must implement all the pure virtual methods from ancestor classes.
For example, Rectangle is missing the scope method inherited from Shape2D, and the area method inherited from Shape. That will cause errors along the lines of "can't instantiate Rectangle class because it is abstract." It looks like you have the implementations of those methods lower in the file, but they aren't declared anywhere inside the Rectangle class.