编程题:
1.完成求三个长方体体积的程序。(要求有构造函数、拷贝构造函数、析构函数,带默认形参值的函数,内敛函数)
2.完成对学生类的抽象(数据成员至少包括学号,姓名,性别,成绩;函数成员至少包括取得这些数据成员的函数。要求有构造函数、拷贝构造函数、析构函数,带默认形参值的函数,内敛函数)
第五章补充题目 1. #include <iostream> using namespace std;
class Width {
public:
Width() { ++count; }
~Width() { --count; }
static int count; };
int Width::count = 0;
int main() {
Width w, x;
cout << "现在是:
" << w.count << " Widths.\n";
{ Width w, x, y, z;
cout << "现在是:
" << w.count << " Widths.\n";
}
cout << "现在是:
" << w.count << " Widths.\n";
Width y;
cout << "现在是:
" << w.count << " Widths.\n";
return 0; } 2. #include <iostream> using namespace std;
class Width { public:
Width() { ++count; }
~Width() { --count; }
static int numWidths () { return count; }
private:
static int count; };
int Width::count = 0;
int main() { cout << "现在是:
" << Width::numWidths () << " Widths.\n";
Width w, x;
cout << "现在是:
" << Width::numWidths () << " Widths.\n";
{ Width w, x, y, z;
cout << "现在是:
" << Width::numWidths () << " Widths.\n";
}
cout << "现在是:
" << Width::numWidths () << " Widths.\n";
Width y;
cout << "现在是:
" << Width::numWidths () << " Widths.\n";
return 0;
} 3. #include<iostream> using namespace std; class Base { private:
double x,y;
const double p; public:
Base(double m,double n,double d):p(d)
{ x=m;y=n;}
void Show();
void Show() const; }; void Base::Show()
{ cout<<x<<","<<y<<"p="<<p<<endl;} void Base::Show() const { cout<<x<<","<<y<<"const p="<<p<<endl;} void main() {
Base a(8.9,2.5,3.14);
const Base b(2.5,8.9,3.14);
b.Show();
a.Show(); } 4. #include <iostream> using namespace std; class Two {
int y; public:
friend class One;
}; class One {
int x; public:
One(int a,Two &r,int b)
{x=a;r.y=b;}
void Display(Two & r)
{cout<<x<<" "<<r.y<<endl;} }; int main() {
Two ob2;
One ob1(22,ob2,30);
ob1.Display(ob2);
return 0; } 5.
#include <iostream> using namespace std; class Exam {
int n; public:
Exam(int i) {n=i;}
void Disp() {cout<<"The first n="<<n<<endl;}
void Disp() const {cout<<"The Second n="<<n<<endl;} };
int main() {
Exam a(10);
const Exam b(20);
a.Disp();
b.Disp();
return 0; } 6. #include <iostream> using namespace std; class Exam { public:
Exam() {cout<<"Calling Constructor"<<endl;} }; void fun(int i) {
static Exam e;
cout<<"The i is :"<<i<<endl; }
int main() {
fun(10);
fun(20);
fun(30);
return 0; }