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

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

自學C++,今天自己昨天題目,可是看了半天也不知道該在空格里填什么。。。

自學C++,今天自己昨天題目,可是看了半天也不知道該在空格里填什么。。。

C++
死神的蘋果 2016-08-09 14:14:29
#include <iostream.h>#include <math.h>class Power{?public:??Power(int i){x=i;}??_______________________;?protected:??int x;};class Square:public Power{?public:??__________________________;??void display()??{???cout<<"x="<<x<<endl;???cout<<"x square="<<x*x<<endl;??}};class Cube:public Power{?________________________?};void fun(__________){?p.display();}int main(){?int m;?while(cin>>m)?{??Square squ(m);??Cube cub(m);??fun(squ);??fun(cub);?}?return 0;}?輸入輸入有多組測試數據,每行為一組測試數據,且只有一個正整數。輸出根據程序代碼輸出相應的信息。樣例輸入123樣例輸出x=1x?square=1x=1x?cube=1x=2x?square=4x=2x?cube=8x=3x?square=9x=3x?cube=27請問,Power類中的語句中的Power(int){x=i}是什么,屬于哪個知識點呢?
查看完整描述

2 回答

?
慕運維2420015

TA貢獻6條經驗 獲得超3個贊

那句屬于構造函數,在實例化Power類時調用,x是Power的受保護的成員變量

查看完整回答
反對 回復 2016-08-13
?
aa543187001

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

#include <iostream>

#include <math.h>

using namespace std;


class Power

{

public:

Power(int i){x=i;}

virtual void display()=0;

protected:

int x;

};

class Square:public Power

{

public:

Square(int i):Power(i){};

void display()

{

cout<<"x="<<x<<endl;

cout<<"x square="<<x*x<<endl;

}

};

class Cube:public Power

{

public:

Cube(int i):Power(i){};

void display()

{

cout<<"x="<<x<<endl;

cout<<"x cube="<<x*x*x<<endl;

}

};

void fun(Power &p)

{

p.display();

}

int main()

{

int m;

while(cin>>m)

{

Square squ(m);

Cube cub(m);

fun(squ);

fun(cub);

}

return 0;

}


Power(int){x=i}有參構造函數 ?最主要的是基類派生類那里 繼承

查看完整回答
反對 回復 2016-08-09
  • 2 回答
  • 0 關注
  • 1162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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