zoukankan      html  css  js  c++  java
  • c++-面向对象:类和对象

    类和对象

    #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;
    }
    
  • 相关阅读:
    iot 表索引dump《2》
    heap表和iot表排序规则不同
    Cannot complete the install because one or more required items could not be found.
    iot表输出按主键列排序,heap表不是
    iot 表主键存放所有数据,且按数据插入顺序排序
    iot表和heap表排序规则不同
    org.eclipse.graphiti.ui.editor.DiagramEditorInput.
    Oracle 排序规则
    perl 异步超时 打印错误
    14.6.3 Grouping DML Operations with Transactions 组DML操作
  • 原文地址:https://www.cnblogs.com/ygjzs/p/12074424.html
Copyright © 2011-2022 走看看