| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1447 人关注过本帖
标题:在chlid的类中的构造函数,名字继承的是Father类的,民族继承的是Mother类的 ...
只看楼主 加入收藏
林家的姑娘
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2016-10-16
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:2 
在chlid的类中的构造函数,名字继承的是Father类的,民族继承的是Mother类的,感觉定义错了,但是不知道眼怎么改
#include<iostream.h>
class Father
{
private:
    int age;//爸爸的年龄
    char national;//爸爸的民族
public:
    char name;//爸爸的姓氏
public:
    Father(int a,char na,char n)//Father的构造函数
    {
        age=a;
        national=na;
        name=n;
   
    }
    char get()
    {
        return name;
    }
    void showi()//father的成员函数,输出爸爸的信息
    {
        cout<<"爸爸的姓: "<<name<<"年龄 "<<age<<"民族 "<<national<<endl;
    }
};
class Mother
{
private:
    int age;//妈妈的年龄
    char name;//妈妈的姓氏
public:
    char national;//妈妈的民族
public:
    Mother(char na,char n,int a)//Mather 构造函数
    {
        name=n;
        age=a;
        national=na;
        
    }
    char get()
    {
        return national;
    }
    void showi()//Mother的成员函数,输出妈妈的信息
    {
        cout<<"妈妈的姓:"<<name<<"年龄 "<<age<<"民族" <<national<<endl;
    }
};

class Child:public Father,public Mother//继承爸爸的姓,妈妈的民族
{
public:
    int age;//孩子的年龄
    char national;//孩子的民族
    char name;//孩子的名字
public:
    Father a;
    Mother b;
    Child(char n,char na,int a):Father(a, na, n),Mother(na, n, a)//Child的构造函数            
    {
        age=a;                                              //小孩类继承的是爸爸类的姓,妈妈类的民族该怎么写?
        name=n;
        national=na;                                        //才能然计算机知道?
    }
    void showi()//Child的成员函数,输出孩子的信息         
    {
        cout<<"孩子的姓:"<<name<<"年龄 "<<age<<"民族 "<<national<<endl;
    }
};
void main()
{
    Father F("李",28,"汉");
    Mother M("丽",24,"黎");
    Child C(F.get(),15,M.get());
    F. showi();
    M. showi();
    C. showi();
}
搜索更多相关主题的帖子: national private include father public 
2016-10-25 22:46
鸿蒙之灵
Rank: 4
来 自:异次元裂缝
等 级:贵宾
威 望:11
帖 子:126
专家分:244
注 册:2016-8-22
收藏
得分:20 
Father(int a,char na,char n)   这是你的构造函数,你注意一下你的参数列表顺序,
Father F("李",28,"汉");        再来看看这个,是不是出现了参数顺序不一致而导致的截断的现象???

Father F("李",28,"汉");
Mother M("丽",24,"黎");        回过头来再看这两句,你都在构造函数中定义了name和national是char类型数据,那你的参数列表是否有些问题?回忆一下char型数据和string型数据在初始化时,赋值的区别,即单引号和双引号的运用。

cout<<"妈妈的姓:"<<name<<"年龄 "<<age<<"民族" <<national<<endl;    这一句,你的代码中出现了 cout 和 endl ,为什么前面没有引用 std 命名空间?

void main()        主函数的void类型编译虽然能够通过,但是在理论上这样的写法是错误的,建议养成良好的编程习惯

#include<iostream.h>         这个头文件的写法是否出现了错误(我的是VS 2005环境,可能有影响)因为它已经被 C/C++ 废弃,编译时会出现:无法打开iostream.h头文件   的报错信息,但是我写成这样可以:#include<iostream>


上面是最直观,无关逻辑的错误,下面是我在你的代码的基础上进行了修改,没有用继承,你自己试试用继承的方式再试试

程序代码:
#include "stdafx.h"
#include<iostream>
#include<stdlib.h>
#include<string>
using namespace std;

class Father
{
private:
    int age;//爸爸的年龄
    string national;//爸爸的民族
    string name;//爸爸的姓氏
public:
    Father(){};
    void set(string n,int a,string na)//Father的构造函数
    {
        age=a;
        name=n;
        national=na;
    }
    string getname()
    {
        return name;
    }
    void showi()//father的成员函数,输出爸爸的信息
    {
        cout<<"爸爸的姓: "<<name<<"年龄 "<<age<<"民族 "<<national<<endl;
    }
};
class Mother
{
private:
    int age;//妈妈的年龄
    string name;//妈妈的姓氏
    string national;//妈妈的民族
public:
    Mother(){};
    void set(string n,int a,string na)//Mather 构造函数
    {
        name=n;
        age=a;
        national=na;
    }
    string getnational()
    {
        return national;
    }
    void showi()//Mother的成员函数,输出妈妈的信息
    {
        cout<<"妈妈的姓:"<<name<<"年龄 "<<age<<"民族" <<national<<endl;
    }
};

class Child/*:public Father,public Mother*///继承爸爸的姓,妈妈的民族
{
private:
    int age;//孩子的年龄
    string national;//孩子的民族
    string name;//孩子的名字
public:
    Child(){};
    void set(string n,int a,string na)//Child的构造函数            
    {
       name=n;                                           
       national=na;
       age=a;                                       
    };
    void showi()//Child的成员函数,输出孩子的信息          
    {
        cout<<"孩子的姓:"<<name<<"年龄 "<<age<<"民族 "<<national<<endl;
    }
};
int main()
{
    Father F;
    F.set("",28,"");
    Mother M;
    M.set("",24,"");
    Child C;
    C.set(F.getname(),15,M.getnational());
    F. showi();
    M. showi();
    C. showi();
    system("pause");
    return 0;
}



图片附件: 游客没有浏览图片的权限,请 登录注册


[此贴子已经被作者于2016-10-28 10:16编辑过]


对待编程,要像对待情人一样
2016-10-28 10:15
林家的姑娘
Rank: 1
等 级:新手上路
帖 子:9
专家分:5
注 册:2016-10-16
收藏
得分:0 
回复 2楼 鸿蒙之灵
谢谢
知道了
2016-11-03 22:13
快速回复:在chlid的类中的构造函数,名字继承的是Father类的,民族继承的是Moth ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018626 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved