Hỏi về lớp kế thừa

#include <iostream>
using namespace std;

class Circle{
	protected:
	double radius;
	char* color;
	
	public:
	Circle(double radius, char* color){
		this->color=color;
		this->radius=radius;
	}
	double getRadius(){
		return this->radius;
	}
	double setRadius(double radius){
		this->radius=radius;
	}
	char* getColor(){
		return this->color;
	}
	char* setColor(char* color){
		this->color=color;
	}
	double getArea(){
		return 3.14*(this->radius)*(this->radius);
	}
	
};
class Cylinder: public Circle{
	
	double height;
	public:
		Cylinder(double radius, char* color,double height){
			this->height=height;
		}
		double getVolume(){
			return getArea()*height;
		}
};
int main(){
	Cylinder cylinder1=Cylinder(6,"blue",3);
	cout<<cylinder1.getVolume();
}

em bị lỗi : no matching function for call to Circle::Circle()
dù là em làm trong một file cpp

Constructor của superclass luôn chạy trước subclass :slight_smile:
Bạn có thể điều chỉnh bằng cách thêm pre-initial construction.

4 Likes

bạn có thể chỉ rõ dc ko tại vì mình cũng mới học :innocent:

Viết vầy cho đẹp.

class Circle {
    Circle(double r, char *c): radius{r}, color{c} {}
    double getRadius() const { return radius; }
    //...
};

class Cylinder: public Circle {
    Cylinder(double r, char *c, double h): Circle{r,c}, height{h} {}
    //...
};
3 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?