gpt4 book ai didi

c++ - 我正在用 C++ 编写一个简单的继承程序,但出现了很多错误

转载 作者:行者123 更新时间:2023-11-28 01:40:10 27 4
gpt4 key购买 nike

不知道我哪里出错了。我是继承新手。包括我得到的所有错误:

21  7   D:\My C++ programs\inheritance.cpp  [Error] redefinition of 'class Sharad'
5 7 D:\My C++ programs\inheritance.cpp [Error] previous definition of 'class Sharad'
D:\My C++ programs\inheritance.cpp In function 'int main()':
27 2 D:\My C++ programs\inheritance.cpp [Error] 'Sourabh' was not declared in this scope
28 2 D:\My C++ programs\inheritance.cpp [Error] 's1' was not declared in this scope

代码:

#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class Sharad{
string a,b,c,d,e;
public:
void getinfo()
{
cout<<"Enter your Dad's full name: "<<endl;
cin>>a;
cout<<"Enter your full name: "<<endl;
cin>>b;
}
void showinfo() //Error no. 2-5 7 D:\My C++ programs\inheritance.cpp [Error] previous definition of 'class Sharad'
{
cout<<"Your dad's name is: "<<a<<endl;
cout<<"Your name is: "<<b<<endl;
}
};
class Sharad : public Sourabh //error no. 3- D:\My C++ programs\inheritance.cpp In function 'int main()':27 2 D:\My C++ programs\inheritance.cpp [Error] 'Sourabh' was not declared in this scope
{

};
int main() //error 4- 28 2 D:\My C++ programs\inheritance.cpp [Error] 's1' was not declared in this scope

{
Sourabh s1;
s1.getinfo();
s1.showinfo();
getch();
return 0;
}

最佳答案

class Sharad : public Sourabh

这是错误的方法。 Sourabh 是继承自 Sharad 的新类:

class Sourabh : public Sharad

关于c++ - 我正在用 C++ 编写一个简单的继承程序,但出现了很多错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47515316/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com