欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c++-面向对象:类和对象

程序员文章站 2022-06-11 16:51:26
类和对象 ......

类和对象

#define _crt_secure_no_warnings
#include <iostream>
#include <string.h> 


using namespace std;


struct hero
{
    char name[64];
    int sex;
};

void printhero(struct hero &h)
{
    cout << "hero" << endl;

    cout << "name = " << h.name << endl;
    cout << "sex = " << h.sex << endl;
}

class advhero
{
public://访问控制权限
    char name[64];
    int sex;

    void printhero()
    {
        cout << "advhero" << endl;
        cout << "name = " << name << endl;
        cout << "sex = " << sex << endl;
    }
};


class animal
{
    //{}以内 叫类的内部, 以外叫类的外部
public:
    char kind[64];
    char color[64];

//在public下面定义成员变量和函数 是能够在类的内部和外部都可以访问的。
    void printanimal()
    {
        cout << "kind = " << kind << endl;
        cout << "color = " << color << endl;
    }

    void write()
    {
        cout << kind << "开始鞋子了" << endl;
    }
    void run()
    {
        cout << kind << "跑起来了" << endl;
    }

    //
private:
    //在private下面定义的成员变量和方法只能够在类的内部访问
    
};

int main(void)
{
    hero h;

    strcpy(h.name, "gailun");
    h.sex = 1;
    printhero(h);



    advhero advh;
    strcpy(advh.name, "chunbro");
    advh.sex = 1;

    advh.printhero();

    cout << "-----------" << endl;
    animal dog;

    strcpy(dog.kind, "dog");
    strcpy(dog.color, "yellow");

    animal sheep;

    strcpy(sheep.kind, "sheep");
    strcpy(sheep.color, "white");

    

    dog.write();
    sheep.run();

    return 0;
}

类的封装

一个类类的内部,默认的访问控制权限是private

一个结构体默认的访问控制权限的是public

#define _crt_secure_no_warnings
#include <iostream>

using namespace std;

struct date
{
    int year;
    int month;
    int day;
};

void init_date(struct date & d)
{
    cout << "year, month, day" << endl;
    cin >> d.year;
    cin >> d.month;
    cin >> d.day;
}

//打印data的接口
void print_date(struct date &d)
{
    cout << d.year << "年" << d.month << "月" << d.day << "日" << endl;
}

bool is_leap_year(struct date &d)
{
    if (((d.year % 4 == 0) && (d.year % 100 != 0)) || (d.year % 400 == 0)) {
        return true;
    }
    return false;
}

class mydate
{
public:
    //成员方法 成员函数
    void init_date()
    {
        cout << "year, month, day" << endl;
        cin >> year;
        cin >> month;
        cin >> day;
    }

    //打印data的接口
    void print_date()
    {
        cout << year << "年" << month << "月" << day << "日" << endl;
    }

    bool is_leap_year()
    {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
            return true;
        }
        return false;
    }


    int get_year()
    {
        return year;
    }

    void set_year(int new_year)
    {
        year = new_year;
    }

protected://保护控制权限。在类的继承中跟private有区别,在单个类中,跟private是一抹一样。
private:
    int year;
    int month;
    int day;
};

//一个类类的内部,默认的访问控制权限是private
class hero
{

    int year;
};

//一个结构体默认的访问控制权限的是public
struct hero2
{
    int year;
    void print()
    {

    }
};

int main(void)
{
#if 0
    date d1;

    init_date(d1);
    print_date(d1);
    if (is_leap_year(d1) == true) {
        cout << "是闰年 " << endl;
    }
    else {
        cout << "不是闰年 " << endl;
    }
#endif


    mydate my_date;

    my_date.init_date();

    my_date.print_date();

    if (my_date.is_leap_year() == true)
    {
        cout << "是闰年 " << endl;
    }
    else {
        cout << "不是闰年 " << endl;
    }

    //getter,setter
    cout << my_date.get_year() << endl;
    my_date.set_year(2000);
    cout << my_date.get_year() << endl;

    hero h;
    //h.year = 1000;

    hero2 h2;
    h2.year = 100;

    return 0;
}

面向对象和面向过程

#define _crt_secure_no_warnings
#include <iostream>

using namespace std;

class dog
{
public:
    void eat(char *food)
    {
        cout << name << "³ô" << food << endl;
    }

    char name[64];
};

//ãæïò¹ý³ì
void eat(class dog &dog, char *food)
{
    cout << dog.name << "³ô" << food << endl;
}




int main(void)
{
    dog dog;

    strcpy(dog.name, "¹·");

    eat(dog, "ïè");

    dog.eat("ïè");



    return 0;
}