I'm having a little trouble with inheritance.
Main.cpp
#include <iostream>
#include "Shapeclass.h"
using namespace std;
int main() {
int shapecount, shapetype[200],i,height[200], width[200];
string name;
cout << "AREA CALULATOR";
cout << "Enter Your Name ";
cin >> name;
cout << "Enter the amount of Shapes you want to calculate Area of: ";
cin >> shapecount;
Shape *p1[200];
cout << "Enter 1 for Circle Enter 2 for Rectangle and 3 for Triangle";
for ( i = 0; i < shapecount; i++)
{
cin >> shapetype[i];
}
for ( i = 0; i < shapecount; i++)
{
if (shapetype[i]==1)
{
cout << "Enter Radius of circle";
cin >> width[i];
p1[i] = new sphere(width[i]);
}
else if (shapetype[i] == 2) {
cout << "Enter width of rectangle";
cin >> width[i];
cout << "Enter height of rectangle";
cin >> height[i];
}
else if (shapetype[i] == 3) {
cout << "Enter base of triangle";
cin >> width[i];
cout << "Enter height of triangle";
cin >> height[i];
}
}
}
Shapeclass.h
#include <iostream>
using namespace std;
class Shape
{
protected:
double area ;
int height, width;
string nama;
void virtual calculate() = 0;
public:
void display() {
calculate();
}
private:
};
class sphere :public Shape {
void calculate(double height, double width) {
}
};
class rectangle :public Shape {
public:
rectangle(int width1, int height1)
{
width = width1;
height = height1;
}
void calculate(double height, double width) {
area = height * width;
}
};
class triangle :public Shape {
public:
triangle(int width1, int height1)
{
width = width1;
height = height1;
}
void calculate(double height, double width) {
area = height * width*0.5;
}
};
class sphere :public Shape {
public:
sphere(int width1)
{
width = width1;
}
void calculate(double width) {
area = width*width*3.14 ;
}
};
I have no idea why it says object of abstract class type "sphere" is not allowed. Specifically line 42.
I'm trying to initialize pointer array objects but it doesn't want to work.
I'm trying to send the width of the sphere to the class but I can't initialize the value of width with the pointer array. To be more specific.
The Shape::calculate() method is declared as pure virtual:
void virtual calculate() = 0;
So, it must be overloaded in derived classes to have them considered by the compiler as concrete ones you'd able to call.
In your case, the overloaded method would be responsible to determine the area of each shape according to its kind and update the instance property.
for example:
void rectangle::calculate() {
area = height * width;
}
and
void triangle::calculate() {
area = height * width * 0.5;
}
would compute the right area for given shapes.
Shape* s1 = new triangle(w, h);
s1->calculate(); // effective call to triangle::calculate()
Shape* s2 = new rectangle(w, h);
s2->calculate(); // effective call to rectangle::calculate()
Related
class BASIC_SHAPE (abstract)
class BASIC_SHAPE
{
public:
double GET_AREA(double _AREA) { AREA = _AREA; return AREA; }
virtual double CALC_AREA() = 0;
private:
double AREA =0;
};
Class CIRCLE
class CIRCLE:public BASIC_SHAPE
{
public:
CIRCLE() { RADIUS = 0; }
CIRCLE(double _RADIUS) { RADIUS = _RADIUS; }
virtual double CALC_AREA() {
double TEMP2 = 3.14 * pow(RADIUS, 2);
return GET_AREA(TEMP2);
}
private:
double RADIUS;
};
Class TRIANGLE
class TRIANGLE: BASIC_SHAPE
{
public:
TRIANGLE() { BASE = 0; HEIGHT = 0; }
TRIANGLE(double _BASE , double _HEIGHT) : BASE{_BASE}, HEIGHT{_HEIGHT} {}
virtual double CALC_AREA() {
double TEMP = 1 / 2 * (BASE * HEIGHT);
return GET_AREA(TEMP);
}
private:
double BASE, HEIGHT;
MAIN
CIRCLE SHAPE2;
TRIANGLE SHAPE3;
void main()
{
double RAD;
std::cout << "Enter a Circle Radius : ";
std::cin >> RAD;
CIRCLE SHAPE2(RAD);
CIRCLE* LEAD1 = new CIRCLE(RAD);
std::cout << "The Area is : " << LEAD1->CALC_AREA();
double BASE , HEIGHT;
std::cout << "\n\nEnter a Triangle Base : ";
std::cin >> BASE;
std::cout << "\nEnter a Triangle Height : ";
std::cin >> HEIGHT;
TRIANGLE SHAPE3(BASE, HEIGHT);
std::cout << SHAPE3.CALC_AREA();
}
it keeps returning zero when I input the BASE & HEIGHT
I have tried using arrow operator and get it with pointers put nothing worked , I,ve tried use pointers and other methods to give me the answer or the SUM of area but nothing happens . constructors or abstract Class are suspected but IDK how ??
As pointed out in the comments by #rturrado 1/2 is 0. 0 times anything is 0. either use 0.5 or 1/2.0*....
Also, why does your GET_AREA method set the area for the base class and return the set area? You need to have different getters and setters. Getter methods should not set and similarly setter methods should not get.
Also, it would be best if you get in the habit of using managed pointers.
How can I ask the user to input those two numbers and print out the result in C++ here? Please, I need your help. I am new to C++.
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area ()
{ return 0; }
};
class Rectangle: public Polygon {
public:
int area ()
{ return width * height; }
};
class Triangle: public Polygon {
public:
int area ()
{ return (width * height / 2); }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << '\n';
cout << ppoly2->area() << '\n';
return 0;
}
I would suggest you to read about constructors a little bit, you can use cin and cout here. Please find this code and see how I have done for rectangle.
#include <iostream>
using namespace std;
class Polygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b; }
virtual int area ()
{ return 0; }
};
class Rectangle: public Polygon {
private:
int a, b;
public:
Rectangle(){
cout<<"Please enter height and width: ";
cin>>a>>b;
set_values(a,b);
}
int area ()
{ return width * height; }
};
class Triangle: public Polygon {
public:
int area ()
{ return (width * height / 2); }
};
int main () {
Rectangle rect;
Triangle trgl;
Polygon * ppoly1 = ▭
Polygon * ppoly2 = &trgl;
//ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area() << '\n';
cout << ppoly2->area() << '\n';
return 0;
}
I need to use a for_each function to call the print function of each object in the list of objects shapeList. When I put function output as the final parameter of for_each, I get a "cannot determine which instance of overloaded function "output" is intended.
void output(Point* point)
{
point->print();
}
This is my output function for for_each
for_each(shapeList.begin(), shapeList.end(), output);
The for_each statement
I have looked at other solutions that involve using binds and lambdas, but this is a class assignment and I cannot use those methods.
Any help is greatly appreciated.
#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include <algorithm>
#define sz 12
using namespace std;
class Point
{
private:
int x, y;
public:
Point() { }
Point(int a, int b)
:x(a), y(b) { }
// print function is pure virtual and that makes class Point an abstract class
// a pure virtual function can have prototype only without definition
// an abstract class can't be instantiated
// its derived class must override this function in order to be a real class
virtual void print() const = 0;
};
void Point::print() const
{
cout << "\nPoint: ( "
<< x
<< " , "
<< y
<< " )";
}
///////////////////////////////////////////////////////////////////
class Circle : public Point
{
private:
int radius;
public:
Circle() : Point() { }
Circle(int a, int b, int c)
:Point(a, b), radius(c) { }
virtual void print() const;
};
void Circle::print() const
{
cout << "\nCenter of the Circle is at: ";
Point::print();
cout << "\nRadius of the Circle is: "
<< radius;
}
/////////////////////////////////////////////////////////////////////
class Cylinder : public Circle
{
private:
int height;
char color[sz];
public:
Cylinder() { }
Cylinder(int a, int b, int r, int h, char clr[])
: Circle(a, b, r), height(h)
{ strcpy(color, clr); }
virtual void print() const;
};
void Cylinder::print() const
{
Circle::print();
cout << "\nHeight of Cylinder is: "
<< height
<< "\nColor of Cylinder is: "
<< color
<< endl;
}
void load_list(list<Point*>&, char*); //
void output(Point*&);
///////////////////////////////////////////////////////////////
int main()
{
char clr[10];
list<Point*> shapeList;////
load_list(shapeList, clr);
for_each(shapeList.begin(), shapeList.end(), output);
return 0;
}
void load_list(list<Point*>& ptList, char *ch)
{
char type;
int x, y, r, h;
ifstream infile("shapes.txt");
if (!infile)
{
cout << "\nCan not open input file.";
exit(1);
}
infile >> type;
while (infile)
{
if (type == 'c')
{
infile >> x >> y >> r;
ptList.push_back(new Circle(x,y,r));
}
else if (type = 'l')
{
infile >> x >> y >> r >> h >> ch;
ptList.push_back(new Cylinder(x, y, r, h, ch));
}
infile >> type;
}
}
void output(Point* point)
{
point->print();
}
You declare the function to take a pointer by reference(?) And the implementation takes a pointer.
How to correct this code, its not working properly.
length and width is not getting the correct values.
I'm having problem where I declared Rectangle::get().
#include<iostream>
using namespace std;
class Rectangle{
protected:
double length;
double width;
public:
void setter(double len, double wid)
{
length = len;
width = wid;
}
double get()
{
return length*width;
}
};
class Block: public Rectangle{
private:
double heigth;
public:
void setter_h(double hei)
{
heigth = hei;
}
double get_1()
{
return(heigth * Rectangle::get());//this is the problem
}
};
int main()
{
double len, hei, wid;
cout<<"Enter the length: ";
cin>>len;
cout<<"Enter the Width: ";
cin>>wid;
cout<<"Enter the H: ";
cin>>hei;
Rectangle R1;
R1.setter(len,wid);
cout<<"Area: " << R1.get();
Block B1;
B1.setter_h(hei);
cout<<"Volume: "<< B1.get_1();
}
Can someone please help me???
im totaly confused because i think i have written right code but its giving garbage value for volume.
Because you didn't set the width and length for B1. You should also call
B1.setter(len, wid);
before calling B1.get_1().
I'm working on inheritance right now. I have a base class called Shape, and few others as sub class. There is no compilation error. But after when i enter all the coordinates, segmentation error pops out. Inside the driver class, option when when i tried using this d[count].toString();
Shape.h
class Shape
{
protected:
string name;
bool containsWarpSpace;
public:
Shape();
Shape(string, bool);
string toString();
virtual double computeArea();
void setName (string);
// some other codes here
};
Square.h
class Square : public Shape
{
protected:
int *x;
int *y;
int area;
int vertices;
public:
double computeArea();
void setSquare(string, string, int*, int*);
string toString();
};
Square.cpp
void Square::setSquare(string name, string type, int* a, int* b)
{
setName(name);
setContainsWarpSpace (type);
vertices = 4;
x = new int[vertices];
y = new int[vertices];
for (int i = 0; i < vertices; i++)
{
x[i] = a[i];
y[i] = b[i];
}
}
string Square::toString()
{
ostringstream convert;
string s;
string type;
for (int i = 0; i < vertices; i++)
{
convert << "point " << i + 1
<< " ( "
<< x[i] << " , " << y[i]
<< " ) "<< endl;
}
s = convert.str();
return s;
}
Driver class with int main()
class Driver
{
public:
Driver();
Shape *d[];
Square *s;
int count;
int noSquare;
int noRectangle;
int noCross;
void printDetails();
void printPlan();
void option1();
void option2();
void option3();
void option4();
string convertString(string);
};
Driver.cpp. This is the default constructor,
Driver :: Driver()
{
Shape d [MAX];
s = new Square [MAX];
count = 0;
int noSquare = 0;
int noRectangle = 0;
int noCross = 0;
}
Driver::option1()
{
if (shape.compare("square") == 0)
{
tempx = new int[4];
tempy = new int[4];
for (int i = 0; i < 4; i++)
{
int j = i + 1;
cout << "Please enter x-ordinate of pt " << j << ": ";
cin >>tempx[i];
cout << "Please enter y-ordinate of pt " << j << ": ";
cin >>tempy[i];
}
s[noSquare].setSquare(shape,type, tempx,tempy);
d[count] = &s[noSquare];
d[count].toString();
}
}
int main ()
{
option1();
}
Change the way you declared your shape in Driver class. In the header, declare it as :
Shape* d;
and in your CPP initialize it:
d = new Shape[MAX];
Also, since you are doing inheritance and arrays and pointers, you should manage your own destructors. Because if the chil object gets destroyed, it will take parent destructor. Therefore, your destructors should be:
virtual ~Shape();
And square:
virtual ~Square();
In them, delete the pointers:
delete x; // in case of square
delete y;
And when you have arrays:
delete [] d; // in case of driver class
Otherwise it will not free memory properly. That would probably fix your problems.