亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想用C++寫一個程序,求問有了解的嗎?

想用C++寫一個程序,求問有了解的嗎?

猛跑小豬 2021-11-24 21:11:29
設計一個father類,一個mother類和一個child類。期中child類繼承father類和mother類。father類和mother類都包含姓和名兩個數據成員,child類僅包含名數據成員。要求一個child類的對象能夠輸出其父,其母和自己的姓名。
查看完整描述

2 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

#include <string>
#include <iostream>

using namespace std;

//class father;
//class mother;
//class child;

class father
{
public:
father()//構造函數
{
firstname="f";
lastname="F";
}
~father()//析構函數
{
firstname.~string();
lastname.~string();
}
father(const string first,const string last)//帶參數的構造函數
{
}
string getFirst()//取出father的名
{
return firstname;//="f";
}
string getLast()//取出father的姓
{
return lastname;//="F";
}
private:
string firstname;//名
string lastname;//姓
};//此處一定要有分號

class mother
{
public:
mother()//mother的構造函數
{
firstname="m";
lastname="M";
}
~mother()//析構
{
firstname.~string();
lastname.~string();
}
string getFirst()//取出mother的名
{
return firstname;//="m";
}
string getLast()//取出mother的姓
{
return lastname;//="M";
}
private:
string firstname;
string lastname;
};

class child:public father,public mother
{
public:
child(){ firstname="c"; }
~child(){ firstname.~string(); }
void showFa()
{
cout<<"Father's name is:";
cout<<father::getFirst()<<father::getLast()<<endl;
}
void showMo()
{
cout<<"Mother's name is:";
cout<<mother::getFirst()<<mother::getLast()<<endl;
}
void showCh()
{

cout<<"Child's name is:";
cout<<firstname<<father::getLast()<<endl;//子隨父姓
}
private:
string firstname;
};

int main(int argc, char* argv[])
{
child ch;
ch.showFa();
ch.showMo();
ch.showCh();
return 0;
}

出以下錯誤的話在首行加#include "stdafx.h"
e:\workspace\vc\baidu2\baidu2.cpp(102) : fatal error C1010: unexpected end of file while looking for precompiled header directive



查看完整回答
反對 回復 2021-11-28
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

#include <iostream.h>
#include<string.h>
class father{
char firstname[10];
char lastname[10];
public:
void print()
{
cout<<"父親的姓是:"<<firstname<<endl;
cout<<"父親的姓是:"<<lastname<<endl;
}
father()
{
strcpy(firstname,"佐");
strcpy(lastname,"助");
}

};
class mather{
char firstname[10];
char lastname[10];
public:
void print()
{
cout<<"母親的姓是:"<<firstname<<endl;
cout<<"母親的姓是:"<<lastname<<endl;
}
mather()
{
strcpy(firstname,"小");
strcpy(lastname,"櫻");
}
};
class child:public father,mather{
char lastname[10];
public:
void print()
{
father::print();
mather::print();
cout<<"小孩的名是:"<<lastname<<endl;
}
child()
{strcpy(lastname,"宇智波");}
};
int main()
{
child Child;
Child.print();
return 0;
}



查看完整回答
反對 回復 2021-11-28
  • 2 回答
  • 0 關注
  • 358 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號