简单c ++中的单继承程序。 [英] program of Single Inheritance in simple c++.
本文介绍了简单c ++中的单继承程序。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<大>
#包括< iostream.h>
#include< conio.h>
班级学生
{
public:
int rno;
//浮动每;
char name [20];
void getdata()
{
cout<<输入RollNo: - \t;
cin>> rno;
cout<<输入名称: - \t;
cin>> name;
}
};
类标记:公共学生
{
public:
int m1,m2,m3,tot;
浮动每;
void getmarks()
{
getdata();
cout<<输入标记1: - \t;
cin>> m1;
cout<<输入标记2: - \t;
cin>> m2;
cout<<输入标记2: - \t;
cin>> m3;
}
void display()
{
getmarks();
cout<<Roll Not \t Name \t Marks1 \t marks2 \t Marks3 \t Total \t Percentage;
cout<< rno<<\t<< name<<\ t<< m1<<\ t<< m2<< ; \t <<立方米<< \t << TOT<< \t <<每;
}
};
void main()
{
student std;
clrscr();
std.getmarks();
std.display();
getch();
}
解决方案
< blockquote>不,这不起作用:
您已声明 std
作为学生的成员
class,而不是标记
类。因此它无法访问 getmarks
或 display
方法。
尝试更改定义为标记
类的成员 - 然后它将包含所有标记成员,以及所有学生成员。
你所做的有点像说物理textboook是一本书,所以任何书都必须包含有关我的物理课程的信息。
#include < iostream.h >
#include < conio.h >
class student
{
< span class =code-keyword> public :
int rno;
// float per;
char 名称[ 20 ];
void getdata()
{
cout<< 输入RollNo: - \t;
cin>> rno;
cout<< 输入名称: - \t;
cin>> name;
}
};
class 标记: public student
{
public :
int m1,m2,m3,tot;
float per;
void getmarks()
{
getdata();
cout<< 输入标记1: - \t;
cin>> m1;
cout<< 输入标记2: - \t;
cin>> m2;
cout<< 输入标记2: - \t;
cin>> m3;
tot = m1 + m2 + m3;
per = tot / 3;
}
void display()
{
cout<< Roll Not \t Name \t Marks1 \t marks2 \t Marks3 \t Total \t Percentage ;
cout<< rno<< \t<< name< ;< \t<< m1<< \t<< m2<< \t<< m3<< \t<< tot<< \t <<每;
}
};
void main()
{
标记m;
clrscr();
m.getmarks();
m.display();
getch();
}
#include<iostream.h> #include<conio.h> class student { public: int rno; //float per; char name[20]; void getdata() { cout<<"Enter RollNo :- \t"; cin>>rno; cout<<"Enter Name :- \t"; cin>>name; } }; class marks : public student { public: int m1,m2,m3,tot; float per; void getmarks() { getdata(); cout<<"Enter Marks 1 :- \t"; cin>>m1; cout<<"Enter Marks 2 :- \t"; cin>>m2; cout<<"Enter Marks 2 :- \t"; cin>>m3; } void display() { getmarks(); cout<<"Roll Not \t Name \t Marks1 \t marks2 \t Marks3 \t Total \t Percentage"; cout<<rno<<"\t"<<name<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<tot<<"\t"<<per; } }; void main() { student std; clrscr(); std.getmarks(); std.display(); getch(); }
解决方案
No, that won't work:
You have declaredstd
as a member of thestudent
class, not amarks
class. So it does not have access to thegetmarks
ordisplay
methods.
Try changing the definition to be a member of themarks
class - it will then have all the members of marks, plus all the members of student.
What you have done is a bit like saying "A physics textboook is a book, so any book must contain information about my physics course".
#include<iostream.h> #include<conio.h> class student { public: int rno; //float per; char name[20]; void getdata() { cout<<"Enter RollNo :- \t"; cin>>rno; cout<<"Enter Name :- \t"; cin>>name; } }; class marks : public student { public: int m1,m2,m3,tot; float per; void getmarks() { getdata(); cout<<"Enter Marks 1 :- \t"; cin>>m1; cout<<"Enter Marks 2 :- \t"; cin>>m2; cout<<"Enter Marks 2 :- \t"; cin>>m3; tot=m1+m2+m3; per=tot/3; } void display() { cout<<"Roll Not \t Name \t Marks1 \t marks2 \t Marks3 \t Total \t Percentage"; cout<<rno<<"\t"<<name<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<tot<<"\t"<<per; } }; void main() { marks m; clrscr(); m.getmarks(); m.display(); getch(); }
这篇关于简单c ++中的单继承程序。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文