首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我正在用C++编写一个简单的继承程序,但是我得到了很多错误

我正在用C++编写一个简单的继承程序,但是我得到了很多错误
EN

Stack Overflow用户
提问于 2017-11-28 00:15:09
回答 1查看 57关注 0票数 0

不知道我搞错了什么。我对继承还不熟悉。包括我得到的所有错误:

代码语言:javascript
复制
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

代码:

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

发布于 2017-11-28 00:23:04

代码语言:javascript
复制
class Sharad : public Sourabh

这是错误的方向。Sourabh是从Sharad继承的新类

代码语言:javascript
复制
class Sourabh : public Sharad
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47515316

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档