【发布时间】:2021-12-10 03:30:21
【问题描述】:
#include <iostream>
using namespace std;
class student
{
protected:
string name;
int roll;
int age;
public:
student(string n, int r, int a)
{
name = n;
roll = r;
age = a;
}
};
class test : public student
{
protected:
int sub[5];
public:
void marks()
{
cout << "Enter marks in 5 subjects: " << endl;
cin >> sub[0] >> sub[1] >> sub[2] >> sub[3] >> sub[4];
}
void display()
{
cout << "Name : " << name << "\nRoll number : " << roll << "\nAge: " << age << endl;
cout << "Marks in 5 subjects : " << sub[0] << ", " << sub[1] << ", " << sub[2] << ", " << sub[3] << ", " << sub[4] << endl;
}
};
class sports
{
protected:
int sportmarks;
public:
sports(int sm)
{
sportmarks = sm;
}
};
class result : public test, public sports
{
int tot;
float perc;
public:
void calc()
{
tot = sportmarks;
for(int i = 0; i < 5; i++)
tot = tot + sub[i];
perc = (tot / 600.0) * 100;
cout << "Total: " << tot << "\nPercentage: " << perc << endl;
}
};
int main()
{
student ob1("Name", 781, 19);
sports ob2(78);
result ob;
ob.marks();
ob.display();
ob.calc();
}
有人要求我使用参数化构造函数来解决这个问题,并注意到我在使用构造函数时遇到了这些错误。如果这篇文章不方便阅读,请见谅。从参数化构造函数创建对象时,我可以通过什么方式运行此代码?
use of deleted function 'result::result()'
use of deleted function 'test::test()'.
no matching function for call to 'student::student()'
no matching function for call to 'sports::sports()'
【问题讨论】:
-
在您的
main()中有result ob;。这是默认构造的请求。class result根本没有构造函数。在这种情况下,编译器会为您生成一个默认构造函数。它构建调用所有基类和所有成员的默认构造函数。所以,我们到了。
标签: c++ inheritance constructor