我必须创建一个具有子类的Shape,用于查找体积的特定形状。我们必须能够构造对象并将它们存储在链表中。我正在测试创建一个对象,我想知道它为什么不运行我的函数cout命令。任何帮助将不胜感激。感谢
#include <iostream>
#include <array>
using namespace std;
class Shape{
public:
friend class my_list;
Shape(double a=0,double b=0 ,double c=0):width(a),height(b),length(c){
}
protected:
double width;
double height;
double length;
Shape *nextPtr;
};
class Rectangle:public Shape
{
public:
Rectangle(double a, double b,double c):Shape(a,b,c){
area = width * length;
};
private:
double area;
};
class Circle:public Shape
{
public:
Circle(double a):Shape(a){
area = (radius*radius)*3.14;
}
protected:
double area;
double radius;
};
class Triangle:public Shape
{
public:
Triangle(double a, double b,double c):Shape(a,b,c){
area = .5*width*length;
};
protected:
double area;
};
class Cubic:public Rectangle
{
public:
Cubic(double a, double b,double c):Rectangle(a,b,c){
volume = width * length * height;
};
protected:
double volume;
};
class Sphere:public Circle
{
public:
Sphere(double a):Circle(a){
volume = (width*width*width)*(4/3)*3.14;
};
protected:
double volume;
};
class Cone:public Circle{
public:
Cone(double a, double b):Circle(a){
height = b;
volume = 3.14*(width*width)*(height/3);
};
protected:
double volume;
};
class my_list{
public:
my_list();
~my_list();
void add_node(Shape*);
void print_list();
Shape* make_node();
private:
Shape* firstPtr;
Shape* lastPtr;
};
my_list::my_list():firstPtr(NULL), lastPtr(NULL){}
my_list::~my_list(){}
Shape* my_list::make_node(){
cout << "What shape would you like to construct?" << endl;
cout << "\t1. Rectangle" << "\n\t2. Triangle" << "\n\t3. Cirlce" <<
"\n\t4. Cubic" <<"\n\t5. Sphere" << "\n\t6. Cone" << endl;
int number; cin >> number; cin.get();
cout << "Enter width/height/length or radius, or radius/height" << endl;
double a,b,c; cin >> a; cin>>b; cin>> c;
switch (number) {
case 1: {
Shape *newPtr = new Rectangle(a,b,c);
return newPtr;
}
case 2: {
Shape *newPtr = new Triangle(a,b,c);
return newPtr;
}
case 3: {
Shape *newPtr = new Circle(a);
return newPtr;
}
case 4: {
Shape *newPtr = new Sphere(a);
return newPtr;
}
case 5: {
Shape *newPtr = new Cubic(a,b,c);
return newPtr;
}
case 6:{
Shape *newPtr = new Cone(a,b);
return newPtr;
}
default:
return 0;
}
}
void my_list::add_node(Shape* new_node){
if ( firstPtr == NULL ) // empty list
firstPtr = lastPtr = new_node; // nextPtr = NULL by default
else {
new_node -> nextPtr = firstPtr;
firstPtr = new_node; // set first pointer to beginning of the list;
}
}
int main(){
/*
while(true) {
cout << endl << endl << "\tSelect the following options" << endl;
cout << "\t1. Construct" << "\n\t2. Add" << "\n\t3. Print" <<
"\n\t4. Quit" << endl;
cout << "\tSelection:\t";
int options; cin >> options; cin.get(); // cin leaves newline, cin.get() to cancel it
switch (options) {
case 1: //construct function
case 2: {
//add function
}
case 3:
//print function
case 4:
default: break;
}
}
*/
my_list l1;
l1.add_node(l1.make_node());
}
答案 0 :(得分:0)
指点:
在调用my_list上的任何函数之前尝试创建它的对象。 比如my_list l1 = new my_list();
在switch-case使用中使用break而不是return。 喜欢:
switch(n){ 情况1: ... 打破;
案例2: ... 打破; }
3.启动IDE并添加断点进行调试。