c++ - 如何在外部类中调用内部类的函数?
问题描述
class student
{
private:
int admno;
char sname[20];
class Student_Marks
{
private:
float eng, math, science, computer, Hindi;
float total;
public:
void sMARKS()
{
cin >> eng >> math >> science >> computer >> Hindi;
}
float cTotal()
{
total = eng + math + science + computer + Hindi;
return total;
}
};
public:
void showData()
{
cout << "\n\nAdmission Number :" << admno;
cout << "\nStudent Name :" << sname;
cout << "\nTotal Marks :" << cTotal();
}
};
我想cTotal()
在外部类函数中调用内部类函数showData()
。
我在访问外部类中的内部类函数时出错。
解决方案
只要您将其称为“嵌套类”而不是内部类,您就可以在语言指南中找到适当的参考。它只是封闭类范围内的类型定义,您必须创建此类的实例才能使用。例如
class student
{
private:
int admno;
char sname[20];
class Student_Marks
{
private:
float eng,math,science,computer,Hindi;
float total;
public:
void sMARKS()
{
cout<<"Please enter marks of english,maths,science,computer,science and hindi\n ";
cin>>eng>>math>>science>>computer>>Hindi;
}
float cTotal()
{
total=eng+math+science+computer+Hindi;
return total;
}
};
Student_Marks m_marks; // marks of this student
您的代码的另一个问题是您输入输入的方法非常缺乏错误检查......
推荐阅读
- c# - 如果我有对象 ID,如何使用 REST API 在 AzureAD 中获取对象的显示名称?
- javascript - 浏览器窗口关闭后使用 javascript 或 Vuejs 运行注销功能
- php - 在页面加载时加载 HTML 表单的提交条件
- arrays - 如何在 Mongoose 中搜索填充数组 [包含 ref 的数组]
- user-interface - 无法让 ui_guage 在指定的延迟时间后显示每条消息
- libcurl - libcurl 使用多接口时如何知道传输已完成?
- java - 在不忽略 SSL 错误的情况下,Android Web 视图中握手失败 SSL 错误的最佳解决方案
- reactjs - 如何在react js中的图像加载事件回调中使用函数?
- c++ - 在二维数组中查找“x”数时出现 C++ 错误
- java - Hibernate 中的 LocalDate 到日期转换问题:ORA-00932:不一致的数据类型:应该是:收到的 NUMBER:BINARY