C艹之路 1.3c--

3.5

例子3.1p70

//;使用构造函数修改例子2.3
#include<iostream>
using namespace std;
class Time
{
public:
    Time() {
        hour = 0;
        minute = 0;
        sec = 0;
    }
    void setTime();
    void showTime();
private:
    int hour;
    int minute;
    int sec;
};
void Time::setTime()
{
    cin >> hour >> minute >> sec;
}
void Time::showTime()
{
    cout << hour << ":" << minute << ":" << sec << endl;
}
int main()
{
    Time t1;
    t1.setTime();
    t1.showTime();
    Time t2;
    t2.showTime();
    return 0;
}

例子3.2p72

//两个长方体,已知各自参数,求体积
#include<iostream>
using namespace std;
class Box
{
public:
    Box(int, int, int);
    int volume();
private:
    int height;
    int width;
    int length;
};
Box::Box(int h, int w, int len)
{
    height = h;
    width = w;
    length = len;

}
int Box::volume()
{
    return(height*width*length);
}
int main() {
    Box box(12, 25, 30);
    cout << "The volume of box is " << box.volume() << endl;
    Box box2(15, 30, 21);
    cout << "The volume of box2 is " << box2.volume() << endl;
    return 0;
}

例子3.3p74

//;在例子3.2基础上,构建两个一个无参数,一个有参数
#include<iostream>
using namespace std;
class Box
{
public:
    Box();
    Box(int h, int w, int len) :height(h), width(w), length(len) {}
    int volume();
private:
    int height;
    int width;
    int length;
};
Box::Box()
{
    height = 10;
    width = 10;
    length = 10;
}
int Box::volume()
{
    return(height*width*length);

}
int main()
{
    Box box1;
    cout << "The volume of box1 is " << box1.volume() << endl;
    Box box2(15,30,25);
    cout << "The volume of box2 is " << box2.volume() << endl;
    return 0;
}

例子3.4p

//在例子3.3基础上,构造函数改用含默认参数的,且全为10
#include<iostream>
using namespace std;
class Box
{
public:
    Box(int h = 10, int w = 10, int len = 10);
    int volume();
private:
    int height;
    int width;
    int length;
};
Box::Box(int h, int w, int len)
{
    height = h;
    width = w;
    length = len;
}
int Box::volume()
{
    return (height*width*length);
}
int main()
{
    Box box1;
    cout << "The volume of box1 is " << box1.volume() << endl;
    Box box2(15);
    cout << "The volume of box2 is " << box2.volume() << endl;
    Box box3(15, 30);
    cout << "The volume of box3 is " << box3.volume() << endl;
    Box box4(15, 30, 20);
    cout << "The volume of box4 is " << box4.volume() << endl;

    return 0;
}

例子3.5p79

//;包含析构函数和构造函数//有问题
#include<string>
#include<iostream>
using namespace std;
class Student
{
public:
    Student(int n, string nam, char s)
    {
        num = n;
        name = nam;
        sex = s;
        cout << "Constructor called" << endl;
    }
    ~Student()
    {
        cout << "Destructor called" << endl;
    }
    void display()
    {
        cout << "num:" << num << endl;
        cout << "name:" << name << endl;
        cout << "sex:" << sex << endl << endl;
    }
private:
    int num;
    string name[20];
    char sex;
};
int main()
{
    Student stu1(10010, "Wang_li", 'f');
    stu1.display();
    Student stu2(10011, "Zhang_fan", 'm');
    stu2.display();
    return 0;
}

例子3.6p

//对象数组:计算和输出3个立方体的体积//书上的错误的修改过来了
#include<iostream>
using namespace std;
class Box
{
public:
    Box(int h = 10, int w = 12, int len = 15) :height(h), width(w), length(len) {}
    int volume();
private :
    int height;
    int width;
    int length;
};
int Box::volume()
{
    return (height*width*length);
}
int main()
{
    Box a[3] = {
        Box(10,12,15),
        Box(15,18,20),
        Box(16,20,26)
        //return 0;
    };
    cout << "volume of a[0] is " << a[0].volume() << endl;
    cout << "volume of a[1] is " << a[1].volume() << endl;
    cout << "volume of a[2] is " << a[2].volume() << endl;

}

例子3.7p87

这里很重要,要细心理解

//适用对象指针输出时分秒//这里很重要,要细心理解
#include<iostream>
using namespace std;
class Time
{
public:
    Time(int, int, int);
    int hour;
    int minute;
    int sec;
    void getTime();
private:

};
Time::Time(int h, int m, int s)
{
    hour = h;
    minute = m;
    sec = s;
}
void Time::getTime() {
    cout << hour << ":" << minute << ":" << sec << endl;
}
int main()
{
    Time t1(10, 13, 56);
    int *p1 = &t1.hour;//为何这里有&
    cout << *p1 << endl;

    t1.getTime();
    Time *p2 = &t1;
    p2->getTime();

    void(Time:: *p3)();
    p3 = &Time::getTime;//为什么加了()之后就报错呢
    (t1.*p3)();

    return 0;

}

例子3.8p97

//使用对象的引用,输出时分秒//可以通过类外函数(参数必须为引用,否则无效),修改相关参数
#include<iostream>
using namespace std;
class Time {
public:
    Time(int, int, int);
    int hour;
    int minute;
    int sec;
};
Time::Time(int h, int m, int s)
{
    hour = h;
    minute = m;
    sec = s;
}
void fun(Time &t)//&必须要有,否则无效
{
    t.hour = 18;
    //t.minute = 30;
}
int main()
{
    Time t1(10, 13, 56);//此时里面参数10,已经被该对象的引用给覆盖了
    fun(t1);
    cout << t1.hour<<":"<<t1.minute<<":"<<t1.sec << endl;
    return 0;
}

例子3.9p100

//对象赋值给另外个对象(必须同个类才行)
#include<iostream>
using namespace std;
class Box
{
public:
    Box(int = 10, int = 10, int = 10);//这里居然不用写出变量名
    int volume();
private:
    int height;
    int width;
    int length;
};
Box::Box(int h, int w, int len)
{
    height = h;
    width = w;
    length = len;
}
int Box::volume()
{
    return (height*width*length);
}
int main()
{
    Box box1(15, 30, 25), box2;//即使没有括号()里面,也能运行
    cout << "The volume of box1 is " << box1.volume() << endl;
    box2 = box1;
    cout << "The volume of box2 is " << box2.volume() << endl;
}

例子3.10p105

//使用静态数据成员输出立方体的体积
#include<iostream>
using namespace std;
class Box
{
public:
    Box(int, int);
    int volume();
    static int height;
    int width;
    int length;
};
Box::Box(int w, int len)
{
    width = w;
    length = len;
}
int Box::volume()
{
    return (height*width*length);
}
int Box::height = 10;
int main()
{
    Box box1(15, 20), box2(20, 30);
    cout << box1.height << endl;
    cout << box2.height << endl;
    cout << Box::height << endl;
    cout << box1.volume()<< endl;
    return 0;
}

例子3.11p107

//使用静态成员函数统计学生平均成绩//没有通过编译
#include<iostream>
using namespace std;
class Student
{
public:
    Student(int n,int a,float s):num(n),age(a),score(s){}
    void total();
    static float average();
private:
    int num;
    int age;
    float score;
    static float sum;
    static int count;
};
void Student::total()
{
    sum += score;
    count++;
}
float Student::average()
{
    return(sum / count);
}
int main()
{
    Student stu[3] =
    {
        Student(1001,18,70),
        Student(1002,18,78),
        Student(1005,20,98)
    };//这里必须添加;因为这是数组
    int n;
    cout << "please input the number of students: ";
    cin >> n;
    for (int i = 0; i < n; i++)
        stu[i].total();
    cout<< "the average score of " << n << "students is " << Student::average() << endl;
    return 0;
}

例子3.12p10


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 220,884评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,212评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 167,351评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,412评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,438评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,127评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,714评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,636评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,173评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,264评论 3 339
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,402评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,073评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,763评论 3 332
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,253评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,382评论 1 271
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,749评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,403评论 2 358

推荐阅读更多精彩内容