我在C 中写了一个简单的继承程序,但是我有很多错误

I am writing a simple inheritance program in C++, but I got many errors

本文关键字:程序 继承 错误 简单 一个 我在      更新时间:2023-10-16

不知道我出了什么问题。我是继承的新手。包括我遇到的所有错误:

21  7   D:My C++ programsinheritance.cpp  [Error] redefinition of 'class Sharad'
5   7   D:My C++ programsinheritance.cpp  [Error] previous definition of 'class Sharad'
D:My C++ programsinheritance.cpp  In function 'int main()':
27  2   D:My C++ programsinheritance.cpp  [Error] 'Sourabh' was not declared in this scope
28  2   D:My C++ programsinheritance.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++ programsinheritance.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++ programsinheritance.cpp   In function 'int main()':27 2   D:My C++ programsinheritance.cpp  [Error] 'Sourabh' was not declared in this scope
 {
 }; 
 int main()       //error 4- 28 2   D:My C++ programsinheritance.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