#include<iostream>usingnamespace std;classbox{public:box(int w =10,int h =10,int l =10):height(h),weigh(w),lengh(l){}intv();private:int hight;int width;int length;};int box::volume(){return(height*length*width);}intmain(){box a[3]={//定义对象数组box(10,12,15),box(15,18,20),box(16,20,26),};
cout <<"v of a[0] is "<< a[0].v()<< endl;
cout <<"v of a[1] is "<< a[1].v()<< endl;
cout <<" v of a[2] is "<< a[2].v()<< endl;}
程序运行结果
v of a[0] is 1800
v of a[1] is 5400
v of a[2] is 8320
Process returned 0(0x0) execution time :0.016 s
Press any key to continue.
输出学生信息
#include<iostream>usingnamespace std;classstudent{public:student(int n, string nam, string s){
num = n;
name = nam;
sex = s;
cout <<"constructor called."<< endl;}~student()//定义析构函数{
cout <<"destructor called."<< endl;}voiddisplay(){
cout <<"num:"<< num << endl;
cout <<"name:"<< name << endl;
cout <<"sex:"<< sex << endl;}private:int num;
string name;
string sex;};intmain(){
student s1(2021524,"张飞","f");
s1.display();
student s2(2021525,"刘备","m");
s2.display();return0;}
程序运行结果
constructor called.
num:2021524
name:张飞
sex:f
constructor called.
num:2021525
name:刘备
sex:m
destructor called.
destructor called.
Process returned 0(0x0) execution time :0.012 s
Press any key to continue.