C++类与对象

使用Windows窗口

#include<stdio.h>
#include<Windows.h>

int main()
{
    MessageBox(NULL, "Mike can drive!", "Driving", MB_YESNO);
    return 0;
}

程序运行的结果如下图所示:


001.JPG

使用面向过程设计方法计算圆的周长和面积

#include<stdio.h>

int main()
{
    double r = 0, girth, area;
    const double PI = 3.14;
    scanf("%lf", &r);
    girth = 2 * PI * r;
    area = PI * r * r;
    printf("%lf,%lf", girth, area);
    while (1);
    return 0;
}

程序的运行结果如图示:


002.JPG

同样的功能采用C++语言的方式实现

#include<iostream>

int main()
{
    double r = 0, girth, area;
    const double PI = 3.14;
    std::cin >> r;
    girth = 2 * PI * r;
    area = PI * r * r;
    std::cout << girth << std::endl;
    std::cout << area << std::endl;
    while (1);
    return 0;
}

运行的结构如图所示:


003.JPG

若采用C++的面向对象设计则程序如下:

#include<iostream>

class Circle {
    double radius;
public:
    double get_radius(double r)
    {
        radius = r;
        return 0;
    }
    double get_girth()
    {
        return 2 * 3.14*radius;
    }
    double get_area()
    {
        return 3.14*radius*radius;
    }
};
int main()
{
    Circle A;
    A.get_radius(5);
    std::cout << "The circle's girth is " << A.get_girth() << std::endl;
    std::cout << "The circle's area is " << A.get_area() << std::endl;
    while (1);
    return 0;
}

程序运行的结构如下:

The circle's girth is 31.4
The circle's area is 78.5

虽然此时程序稍微复杂了一点,但是程序的复用性很高,例如可以直接计算多个圆的相关参数:

int main()
{
    Circle A,B,C;
    A.get_radius(5);
    std::cout << "The circle's girth is " << A.get_girth() << std::endl;
    std::cout << "The circle's area is " << A.get_area() << std::endl;

    B.get_radius(10);
    std::cout << "The circle's girth is " << B.get_girth() << std::endl;
    std::cout << "The circle's area is " << B.get_area() << std::endl;

    C.get_radius(15);
    std::cout << "The circle's girth is " << C.get_girth() << std::endl;
    std::cout << "The circle's area is " << C.get_area() << std::endl;
    while (1);
    return 0;
}

程序的执行结构如下:

The circle's girth is 31.4
The circle's area is 78.5
The circle's girth is 62.8
The circle's area is 314
The circle's girth is 94.2
The circle's area is 706.5

关于公有,私有和保护成员

#include<iostream>
using namespace std;

class animal
{
public:
    char people[10];
    char lion[10];
    char glede[10];
};//类的主体是在{}中间,最后需要加上一个分号

class MyClass
{
//在三者之外即未写修饰符则默认为私有的
//修饰符的作用域是从修饰符开始到下一个访问修饰符或最后
public:
    //公开的,公共的
    int a = 10;
    void print();
private:
    //私有的,类里的成员和函数才可以访问
protected:
    //受保护的,所继承的派生类里可以访问
};

void MyClass::print()
{
    cout << "hello world" << endl;
}

int main()
{
    MyClass A;
    cout << A.a << endl;
    A.print();
    getchar();
    return 0;
}

程序的输出结果如下:

10
hello world

若在私有或者保护成员里面添加项,用对象是调用不了的。

构造函数

class Time
{
public:
    Time();//构造函数声明,函数名与类名一致,没有类型也没有返回值
};

Time::Time()
{
    cout << "This is a constructor" << endl;
}

int main()
{
    Time t;
    getchar();
    return 0;
}

输出结果:

This is a constructor

有参构造

#include<iostream>
using namespace std;

class Max
{
public:
    Max(int a, int b);
//如果不屑构造函数,编译器会自动生成,如果写了,则调用你写的构造函数
//构造函数分为有参和无参两种情况,如果是有参构造,在创建对象的时候记得也要传递参数
    int MaxFunc();
private:
    int x, y;
};

Max::Max(int a, int b)
{
    cout << "This is parameter_constructor" << endl;
    x = a;
    y = b;
}

int Max::MaxFunc()
{
    return x > y ? x : y;
}
int main()
{
    Max m(3,6);
    cout << "the biggest" << m.MaxFunc() << endl;
    getchar();
    return 0;
}

程序的输出结果是:

This is parameter_constructor
the biggest6

构造函数的重载

#include<iostream>
using namespace std;

class Max
{
public:
    Max(int a, int b);//两个参数的构造函数
    Max(int a, int b, int c);//三个参数的构造函数
    int MaxFunc1();
    int MaxFunc2();
private:
    int x;
    int y;
    int z;
};

Max::Max(int a, int b)
{
    cout << "This is two parameter_constructor" << endl;
    x = a;
    y = b;
}

Max::Max(int a, int b, int c)
{
    cout << "This is three parameter_constructor" << endl;
    x = a;
    y = b;
    z = c;
}

int Max::MaxFunc1()
{
    return x > y ? x : y;
}

int Max::MaxFunc2()
{
    return x > y ? (x > z ? x : z) : (y > z ? y : z);
}

int main()
{
    Max m1(3, 6);
    cout << "the biggest" << m1.MaxFunc1() << endl;
    Max m2(3, 6, 9);
    cout << "the biggest" << m2.MaxFunc2() << endl;
    getchar();
    return 0;
}

程序的运行结果是:

This is two parameter_constructor
the biggest6
This is three parameter_constructor
the biggest9

拷贝构造函数

#include<iostream>
using namespace std;

class number
{
public:
    number();//默认构造函数
    number(int i);//有参构造函数
    number(number&copy);//拷贝构造函数

    void print();//输出函数
    void print(number obj);//重载输出函数
private:
    int *p;
};

number::number()
{
    cout << "I am default constructor!" << endl;
}
number::number(int i)
{
    cout << "I am parameter constructor!" << endl;
    p = new int;//给指针p申请内存
    *p = i;//形参赋值给地址p中的值
}
number::number(number&copy)//传递的是一个对象
{
    cout << "I am copy constructor!" << endl;
    p = new int;
    *p = *copy.p;//值拷贝
//copy是形参,*copy.p即为传入对象的值
}

void number::print()
{
    cout << "value:" << *p << endl;
}
void number::print(number obj)
{
    cout << "value:" << *obj.p << endl;
}

int main()
{
    number n1;
    number n2(5);
    n2.print();

    number n3(10);
    n3.print();

    number n4(n2);
    n4.print();

    number n5(n3);
    n5.print();

    getchar();
    return 0;
}

程序的运行结果是:

I am default constructor!
I am parameter constructor!
value:5
I am parameter constructor!
value:10
I am copy constructor!
value:5
I am copy constructor!
value:10

析构函数

class MyClass
{
public:
    MyClass();//构造函数 申请内存 成员函数 没有类型 没有返回值
    ~MyClass();//析构函数 释放内存 成员函数 没有类型 没有返回值 没有参数
};

MyClass::MyClass()
{

}
MyClass::~MyClass()
{

}

例如在上述的程序中添加如下部分:

~number();

number::~number()
{
    cout << "I will delete the storage!" << endl;
}
//去掉getchar()

则控制台运行的结果变为:

I am default constructor!
I am parameter constructor!
value:5
I am parameter constructor!
value:10
I am copy constructor!
value:5
I am copy constructor!
value:10
I will delete the storage!
I will delete the storage!
I will delete the storage!
I will delete the storage!
I will delete the storage!
请按任意键继续. . .

出现四次的原因是因为创建了四次对象

友元

#include<iostream>
using namespace std;

class computer
{
public:
    void getPrice(double f);//普通成员函数,用来获取电脑的价格
    friend void print(computer PC);//友元函数
private:
    double Price;
};

//普通成员函数,用来获取电脑的价格
void computer::getPrice(double f)
{
    Price = f;
}

//友元函数不是成员函数,不需要加作用域
void print(computer PC)
{
    cout << "the price is:" << PC.Price<<endl;
}

int main()
{
    computer PC;
    PC.getPrice(10000);
    print(PC);

    return 0;
}

程序运行的结果是:

the price is:10000
请按任意键继续. . .

友元类

#include<iostream>
using namespace std;

class computer
{
public:
    char Name[20] = "ThinkPad";
    void getPrice(double f);
    friend class Mycomputer;
private:
    double Price=10000;
};

void computer::getPrice(double f)
{
    Price = f;
}


class Mycomputer
{
public:
    computer PC;
    void print();
};

void Mycomputer::print()
{
    cout << "My computer brand is:" << PC.Name << endl;//Name是公共的
    cout << "My computer price is:" << PC.Price << endl;
}


int main()
{
    Mycomputer pc;
    pc.print();

    return 0;
}

程序的执行结果如下:

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

推荐阅读更多精彩内容

  • 1.1面向对象的思想 以计算圆周长和面积的问题为例:面向过程的程序设计语言使用高级语言提供的数值计算工具,从功能入...
    苏字龙阅读 591评论 0 1
  • 类方法 OC 中类的方法只有类的静态方法和类的实例方法 OC 中的方法只要声明在 @interface 里,就可以...
    devZhang阅读 1,024评论 0 0
  • 在C++中通常有main函数以及一个或多个既包含数据成员也包含成员函数的类构成,所以: 在一个类中,可提供一个或多...
    samtake阅读 336评论 0 0
  • 面向对象编程(OOP) 在前面的章节中,我们学习了Kotlin的语言基础知识、类型系统、集合类以及泛型相关的知识。...
    Tenderness4阅读 4,414评论 1 6
  • C++文件 例:从文件income. in中读入收入直到文件结束,并将收入和税金输出到文件tax. out。 检查...
    SeanC52111阅读 2,747评论 0 3