帮一个同事答题,C++语法不是很看得懂,程序在VS里跑总是报错,请各位帮我看一下,万谢!
求程序的运行结果
1
#include
class Widget
{public:
Widget(){++count;}
~Widget(){--count;}
int numWidgets(){return count;}
private:
static int caount;
};
int Widget::count=0;
void main()
{Widget w,x;
cout << "Now there are " << w.numWidgets() << "widgets.\n";
{Widget w,x,y,z;
cout << "Now there are " << w.numWidgets() << "widgets.\n";
}
cout << "Now there are " << w.numWidgets() << "widgets.\n";
Widget y;
cout << "Now there are " << w.numWidgets() << "widgets.\n";
}
2.
#include
class Tdate
{
public:
Tdate(int m=5,int d=16,int y=2013)
{
month=m; day=d; year=y;
cout <<month << "/"<<day <<"/"<year <endl;
}
protected:
int mouth;
int day;
int year;
};
void main()
{
Tdate aday;
Tdate bday(8);
Tdate cday(10,20);
Tdate dday(11,2,2014);
}
3.
#include
class C0
{
public:
int nV;
void fun(){cout << "Member of C0" <<endl;}
};
class C1:public C0
{
public:
int nV1;
void fun(){cout << "Member of C1" <<endl;}
};
class C2:public C0
{
public:
int nV2;
void fun(){cout << "Member of C2" <<endl;}
}
class D1:public C1,public C2
{
public:
int nVd;
void fun(){cout << "Member of D1" <<endl;}
}
void main()
{
D1 d1;
d1.fun();
d1.C1::nV=2;
d1.C1::fun();
d1.C2::nV=3;
d1.C2::fun();
}
第一题
http://codepad.org/vWqeLqrt
Now there are 2widgets.
Now there are 6widgets.
Now there are 2widgets.
Now there are 3widgets.
第二题
http://codepad.org/WWsshfR2
5/16/2013
8/16/2013
10/20/2013
11/2/2014
第三题
http://codepad.org/XOgjJELQ
Member of D1
Member of C1
Member of C2
每个程序代码都有不同程度的错误,都帮你修正了。请及时点我的答案的右侧的采纳。
.。。。。你这代码给的。。。。。首先来说,第一题中,main函数里的大括号是怎么回事。。。第二题答案
5/16/2013
8/16/2013
10/20/2013
11/2/2014
第三题答案
Member of D1
Member of C1
Member of C2
第一题
private:
static int count;
第二题
month=m; day=d; year=y;
cout<<month<<"/"<<day<<"/"<<year<<endl;
第三题
void fun(){cout << "Member of C2" <<endl;}
};
class D1:public C1,public C2
{
public:
int nVd;
void fun(){cout << "Member of D1" <<endl;}
};
都是输入错误