01/10/2018, 10:40

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 << "
Ten: " << name << "
Tuoi: " << age << "
Gioi 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 << "
Ten: " << getName() << "
Tuoi: " << getAge() << "
Gioi tinh: " << getSex() << "
Dia chi: " << address << "
So 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 << "
Ten: " << getName() << "
Tuoi: " << getAge() << "
Gioi tinh: " << getSex() << "
Gio lam viec: " << hour << "
Luong: " << 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 << "
Ten: " << getName() << "
Tuoi: " << getAge() << "
Gioi tinh: " << getSex() << "
Dia chi: " << getAddress() << "
So dien thoai: " << getPhone() << "
Gio lam viec: " << getHour() << "
Luong: " << getSalary() << "
Chuc vu:" << position << endl;
}

int main()
{
	Employee employee("Nguyen Van A", 30, "Nam", "TPHCM", "0987654321", 250, 50000000, "Giam doc");
	cout << "
Thong tin Nhan Vien";
	employee.show(); //phuong thuc cua lop Employee
	cout << "
Thong tin Ca Nhan";
	employee.Person::show(); //phuong thuc cua lop Person
	cout << "
Thong tin Nguoi Lao Dong";
	employee.Worker::show(); //phuong thuc cua lop Worker
	cout << "
Thong tin Nguoi";
	employee.Human::show(); //phuong thuc cua lop Human
	system("pause");
	return 0;
}
Huy Lê viết 12:51 ngày 01/10/2018

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

Dark.Hades viết 12:55 ngày 01/10/2018

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

TeoTony viết 12:43 ngày 01/10/2018

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

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)

Dark.Hades viết 12:52 ngày 01/10/2018

Được thì cho mình xin dấu tích ở dưới ấy
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

Bài liên quan
0