Chương trình không chạy được (Đa kế thừa, lớp cơ sở trừu tượng)

Chương trình em nó báo bị lỗi chỗ dòng này
Employee::Employee(string nameIn, int ageIn, string sexIn, string addressIn, string phoneIn, int hourIn, float salaryIn, string positionIn) : Human(nameIn, ageIn, sexIn), Person(addressIn, phoneIn), Worker(hourIn, salaryIn)

“no instance of constructor “Person::Person” matches the argument list”

#include <iostream>
#include <conio.h>
#include <string>

using namespace std;

class Human
{
private:
	string name; // Ten
	int age; // Tuoi
	string sex; // Gioi Tinh
public:
	void setName(string); // Gan ten
	string getName(); // Doc ten
	void setAge(int); // Gan tuoi
	int getAge(); // Doc tuoi
	void setSex(string); // Gan gioi tinh
	string getSex(); // Doc gioi tinh
	Human(string nameIn = "", int ageIn = 0, string sexIn = ""); // Khoi tao thong tin ve Nguoi
	void show(); // Hien thi thong tin Nguoi
};

void Human::setName(string nameIn)
{
	name = nameIn;
}

string Human::getName()
{
	return name;
}

void Human::setAge(int ageIn)
{
	age = ageIn;
}

int Human::getAge()
{
	return age;
}

void Human::setSex(string sexIn)
{
	sex = sexIn;
}

string Human::getSex()
{
	return sex;
}

Human::Human(string nameIn, int ageIn, string sexIn) // Khai bao phuong thuc ben ngoai lop
{
	name = nameIn;
	age = ageIn;
	sex = sexIn;
}

void Human::show() // Hien thi thong tin nguoi
{
	cout << "\nTen: " << name << "\nTuoi: " << age << "\nGioi tinh: " << sex << endl;
}

class Person : public virtual Human // Dinh nghia lop Person ke thua tu lop Human
{
private:
	string address;
	string phone;
public:
	void setAddress(string);
	string getAddress();
	void setPhone(string);
	string getPhone();
	Person(string nameIn = "", int ageIn = 0, string sexIn = "", string addressIn = "", string phoneIn = ""); // Khoi tao thong tin ve Ca Nhan
	void show(); // Hien thi thong tin Ca Nhan
};

void Person::setAddress(string addressIn)
{
	address = addressIn;
}

string Person::getAddress()
{
	return address;
}

void Person::setPhone(string phoneIn)
{
	phone = phoneIn;
}

string Person::getPhone()
{
	return phone;
}

// Khoi tao du tham so
Person::Person(string nameIn, int ageIn, string sexIn, string addressIn, string phoneIn) :Human(nameIn, ageIn, sexIn)
{
	address = addressIn;
	phone = phoneIn;
}

void Person::show() // Hien thi thong tin Ca Nhan
{
	cout << "\nTen: " << getName() << "\nTuoi: " << getAge() << "\nGioi tinh: " << getSex() << "\nDia chi: " << address << "\nSo dien thoai: " << phone << endl;
}

class Worker : public virtual Human
{
private:
	int hour;
	float salary;
public:
	void setHour(int hourIn);
	int getHour();
	void setSalary(float salaryIn);
	float getSalary();
	Worker(string nameIn = "", int ageIn = 0, string sexIn = "", int hourIn = 0, float salaryIn = 0);
	void show();
};

void Worker::setHour(int hourIn)
{
	hour = hourIn;
}

int Worker::getHour()
{
	return hour;
}

void Worker::setSalary(float salaryIn)
{
	salary = salaryIn;
}

float Worker::getSalary()
{
	return salary;
}

Worker::Worker(string nameIn, int ageIn, string sexIn, int hourIn, float salaryIn) : Human(nameIn, ageIn, sexIn)
{
	hour = hourIn;
	salary = salaryIn;
}

void Worker::show()
{
	cout << "\nTen: " << getName() << "\nTuoi: " << getAge() << "\nGioi tinh: " << getSex() << "\nGio lam viec: " << hour << "\nLuong: " << salary << endl;
}

class Employee : public Person, public Worker
{
private:
	string position;
public:
	void setPosition(string positionIn);
	string getPosition();
	Employee(string nameIn = "", int ageIn = 0, string sexIn = "", string addressIn = "", string phoneIn = "", int hourIn = 0, float salaryIn = 0, string positionIn = "");
	void show();
};

void Employee::setPosition(string positionIn)
{
	position = positionIn;
}

string Employee::getPosition()
{
	return position;
}

Employee::Employee(string nameIn, int ageIn, string sexIn, string addressIn, string phoneIn, int hourIn, float salaryIn, string positionIn) : Human(nameIn, ageIn, sexIn), Person(addressIn, phoneIn), Worker(hourIn, salaryIn)
{
	position = positionIn;
}

void Employee::show()
{
	cout << "\nTen: " << getName() << "\nTuoi: " << getAge() << "\nGioi tinh: " << getSex() << "\nDia chi: " << getAddress() << "\nSo dien thoai: " << getPhone() << "\nGio lam viec: " << getHour() << "\nLuong: " << getSalary() << "\nChuc vu:" << position << endl;
}

int main()
{
	Employee employee("Nguyen Van A", 30, "Nam", "TPHCM", "0987654321", 250, 50000000, "Giam doc");
	cout << "\nThong tin Nhan Vien";
	employee.show(); //phuong thuc cua lop Employee
	cout << "\nThong tin Ca Nhan";
	employee.Person::show(); //phuong thuc cua lop Person
	cout << "\nThong tin Nguoi Lao Dong";
	employee.Worker::show(); //phuong thuc cua lop Worker
	cout << "\nThong tin Nguoi";
	employee.Human::show(); //phuong thuc cua lop Human
	system("pause");
	return 0;
}

Kế thừa hình như là ‘‘virtual public’’

Bạn chưa khai báo hàm construct nào cho person có dạng

Person::Person(std::string&, std::string&)

Bạn mới chỉ khai báo:

Person::Person(string nameIn, int ageIn, string sexIn, string addressIn, string phoneIn)

Sửa lại hàm construct của employee

Employee::Employee(string nameIn, int ageIn, string sexIn, string addressIn, string phoneIn, int hourIn, float salaryIn, string positionIn) : 
  Human(nameIn, ageIn, sexIn), 
  Person(nameIn, ageIn, sexIn, addressIn, phoneIn), 
  Worker(nameIn, ageIn, sexIn, hourIn, salaryIn)

Note: Bạn nên theo quy tắc 80x25 để nhìn code dễ hơn, đọc code bạn mà thanh scroll kéo khét hết màn hình :joy:

3 Likes

Mình chỉ sửa giống như cái này là chạy được
Không cần khai báo gì đó luôn

Được thì cho mình xin dấu tích ở dưới ấy :smiley:
Vì bạn viết sai hàm constructor của Persion nên nó báo lỗi, bạn phải sửa lại các tham biến giống với hàm construct thì nó mới chạy.
Trường hợp không sửa thì phải overload function

4 Likes
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?