類和對象 ...
類和對象
#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;
}