當前位置:編程學習大全網 - 編程軟體 - 編壹個復雜的c++程序

編壹個復雜的c++程序

#include <string>

#include <iostream>

using namespace std;

class Fruit //定義壹個類,名字叫Fruit

{

string name; //定義壹個name成員

string colour; //定義壹個colour成員

public:

friend istream& operator>>(istream&,Fruit&); //必須要聲明為友元啊,不然怎麽輸入啊

friend ostream& operator<<(ostream&,const Fruit&); //同理

void print() //定義壹個輸出名字的成員print()

{

cout<<colour<<" "<<name<<endl;

}

Fruit(const string &nst = "apple",const string &cst = "green"):name(nst),colour(cst)

{

} //構造函數

~Fruit()

{

}

};

ostream& operator<<(ostream &out,const Fruit &s) //我是輸出操作符的重載

{

out<<s.colour<<" "<<s.name;

return out;

}

istream& operator>>(istream& in,Fruit &s) //我是輸入操作符的重載

{

in>>s.colour>>s.name;

if(!in)

cerr<<"Wrong input!"<<endl;

return in;

}

int main()

{

Fruit apple;

cin >>apple;

cout<<apple;

return 0;

}

  • 上一篇:環形圈側壁孔口r0.5倒角鉗工怎麽修
  • 下一篇:體彩中心的專管員是幹什麽的
  • copyright 2024編程學習大全網