C++读程序写答案(简单)

[复制链接]
查看11 | 回复2 | 2010-7-7 02:48:30 | 显示全部楼层 |阅读模式
1.
#include
class Simple{

int x,y;
public:

Simple() {x=y=0;}

Simple(int i,int j) {x=i;y=j;}

void copy(Simple&s);

void setxy(int i,int j) {x=i;y=j;}

void print() {cout<<"x="<<x<<",y="<<y<<endl;}
};
void Simple::copy(Simple&s)
{
x=s.x; y=s.y;
}
void func(Simple s1,Simple&s2)
{
s1.setxy(30,40);

s2.setxy(70,80);
}
void main()
{
Simple obj1(1,2),obj2;

obj2.copy(obj1);

func(obj1,obj2);

obj1.print();

obj2.print();
}
2.
#include
class Complex{

double re,im;
public:

Complex(double r,double i):re(r),im(i){}

double real()const {return re;}

double image()const {return im;}

Complex& operator+=(Complex a)

{re+=a.re;

im+=a.im;

return*this;

}
};
ostream&operator<<(ostream&s,const Complex&z)
{return s<<'('<<z.real()<<','<<z.image()<<')';}
int main()
{Complex x(1,-2),y(2,3);
cout<<(x+=y)<<endl;
return 0;
}
3.
#include
class Toy{
public:

Toy(char* _n) {strcpy(name,_n);count++;}

~Toy() {count--;}

char* GetName() {return name;}

static int getCount() {return count;}
private:

char name[10];

static int count;
};
int Toy::count=0;
int main(){

Toy t1("Snoopy"),t2("Mickey"),t3("Barbie");

cout<<t1.getCount()<<endl;

return 0;
}
4.
#include
class Base{
public:

void fun1() {cout<<"Base\n";}

virtual void fun2() {cout<<"Base\n";}
};
class Derived:public Base{
public:

void fun1() {cout<<"Derived\n";}

void fun2() {cout<<"Derived\n";}
};
void f(Base&b) {b.fun1(); b.fun2();}
int main()
{
Derived obj;

f(obj);

return 0;
}

回复

使用道具 举报

千问 | 2010-7-7 02:48:30 | 显示全部楼层
1.x=1,y=2x=70,y=802.(3,1)3.34.BaseDerived 其实这个用编译器就可以搞定的了,用不着花分上了问的^_^
回复

使用道具 举报

千问 | 2010-7-7 02:48:30 | 显示全部楼层
1.x=1,y=2x=70,y=802.(3,1)3.34.BaseDerived
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行