C++基础04-类基础
?一、類和對(duì)象
面向?qū)ο笕筇攸c(diǎn):封裝、繼承、多態(tài)。
struct 中所有行為和屬性都是 public 的(默認(rèn))。C++中的 class 可以指定行為和屬性的訪問(wèn)方式。
封裝,可以達(dá)到,對(duì)內(nèi)開放數(shù)據(jù),對(duì)外屏蔽數(shù)據(jù),對(duì)外提供接口。達(dá)到了信息隱蔽的功能。
class 封裝的本質(zhì),在于將數(shù)據(jù)和行為,綁定在一起然后能過(guò)對(duì)象來(lái)完成操作?。
#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>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://訪問(wèn)控制權(quán)限char name[64];int sex;void printHero(){cout << "advHero" << endl;cout << "name = " << name << endl;cout << "sex = " << sex << endl;} };void test01() {Hero h;strcpy(h.name, "gailun");h.sex = 1;printHero(h);AdvHero advH;strcpy(advH.name, "ChunBro");advH.sex = 1;advH.printHero(); }class Animal {//{}以內(nèi) 叫類的內(nèi)部, 以外叫類的外部 public:char kind[64];char color[64];//在public下面定義成員變量和函數(shù) 是能夠在類的內(nèi)部和外部都可以訪問(wèn)的。void printAnimal(){cout << "kind = " << kind << endl;cout << "color = " << color << endl;}void write(){cout << kind << "開始鞋子了" << endl;}void run(){cout << kind << "跑起來(lái)了" << endl;}// private://在private下面定義的成員變量和方法只能夠在類的內(nèi)部訪問(wèn)}; void test02() {Animal dog;strcpy(dog.kind, "dog");strcpy(dog.color, "yellow");Animal sheep;strcpy(sheep.kind, "sheep");strcpy(sheep.color, "white");dog.write();sheep.run(); } int main(void) {test01();cout << "-----------" << endl;test02();return 0; } #endif二、類的封裝
#if 1 #define _CRT_SECURE_NO_WARNINGS #include <iostream>//struct 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; } void test01() {Date d1;init_date(d1);print_date(d1);if (is_leap_year(d1) == true) {cout << "是閏年 " << endl;}else {cout << "不是閏年 " << endl;} }//類 class MyDate { public://成員方法 成員函數(shù)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://保護(hù)控制權(quán)限。在單個(gè)類中,跟private是一抹一樣。//在類的繼承中跟private有區(qū)別, private:int year;int month;int day; };void test02(){MyDate my_date;my_date.init_date();my_date.print_date();if (my_date.is_leap_year() == true){cout << "是閏年 " << endl;}else {cout << "不是閏年 " << endl;}//getter,settercout << my_date.get_year() << endl;my_date.set_year(2000);cout << my_date.get_year() << endl; }//一個(gè)類類的內(nèi)部,默認(rèn)的訪問(wèn)控制權(quán)限是private class Hero {int year; };//一個(gè)結(jié)構(gòu)體默認(rèn)的訪問(wèn)控制權(quán)限的是public struct Hero2 {int year;void print(){} };void test03() {Hero h;//h.year = 1000; 報(bào)錯(cuò)Hero2 h2;h2.year = 100; } int main(void) {//test01();test02();test03();return 0; } #endif三、面向過(guò)程和面向?qū)ο?/h3> #if 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]; };//面向過(guò)程 void eat(class Dog &dog, char *food) {cout << dog.name << "吃" << food << endl; }int main(void) {Dog dog;strcpy(dog.name, "狗");//過(guò)程驅(qū)動(dòng)eat(dog, "翔");//對(duì)象驅(qū)動(dòng)dog.eat("翔");return 0; } #endif
練習(xí):
#if 1 #define _CRT_SECURE_NO_WARNINGS #define PI 3.14 #include <iostream> using namespace std;//面向過(guò)程 double get_l(double r) {return 2 * PI*r; } double get_s(double r) {return PI*r*r; } void test01() {cout << "面向過(guò)程" << endl;double r = 2;cout << "周長(zhǎng)為:" << get_l(r) << endl;cout << "面積為:" << get_s(r) << endl; }//面向?qū)ο?class Circle { public:Circle() {}Circle(double r) :m_r(r) {}void setR(double r) {m_r = r;}double get_l() {return 2 * PI*m_r;}double get_s() {return PI*m_r*m_r;} public:double m_r; //我 double m_r; }; void test02() {cout << "面向?qū)ο?#34; << endl;Circle c1(2);cout << "周長(zhǎng)為:" << c1.get_l() << endl;cout << "面積為:" << c1.get_s() << endl;Circle c2;c2.setR(4);cout << "周長(zhǎng)為:" << c2.get_l() << endl;cout << "面積為:" << c2.get_s() << endl;} class Circle2 { public:void setR(double r) {m_r = r;}double get_l() {return m_l;}double get_s() {return m_s;} private:double m_r;double m_l= 2 * PI*m_r; //其實(shí)為2*3.14*隨機(jī)數(shù) 錯(cuò)誤double m_s= PI*m_r*m_r; //其實(shí)為3.14*隨機(jī)數(shù)*隨機(jī)數(shù) 錯(cuò)誤 }; void test03() {Circle2 c1;c1.setR(10);cout << c1.get_l() << endl; //-5.81274e+62cout << c1.get_s() << endl; //2.69013e+124 } class Circle3 { public:void setR(double r) {m_r = r;}double get_l() {return 2 * PI*m_r;}double get_s() {return PI*m_r*m_r;} private:double m_r;double m_l ; //其實(shí)為2*3.14*隨機(jī)數(shù) 錯(cuò)誤double m_s ; //其實(shí)為3.14*隨機(jī)數(shù)*隨機(jī)數(shù) 錯(cuò)誤 }; void test04() {Circle3 c1;c1.setR(10);cout << c1.get_l() << endl; //62.8cout << c1.get_s() << endl; //314 } int main() {test01();test02();cout << "-------------" << endl;test03();cout << "-------------" << endl;test04();return 0; } #endif // demo02_circle_err.cpp #include<iostream> using namespace std;//c++的命名空間 class circle { public: double r; double pi = 3.1415926; double area = pi*r*r; }; int main(void) { circle pi; cout<<"請(qǐng)輸?area"<< endl; cin >>pi.r; cout<<pi.area<<endl; //亂碼 return 0; }運(yùn)行結(jié)果:?
#include "iostream" #define PI 3.1415926 using namespace std; class Cricle { public:double m_r;double m_s; public:void setR(double r) {m_r = r;}double getR(){return m_r;}double getS(){m_s = m_r*m_r*PI;return m_s;} }; int main() {double r1;Cricle c1;cout << "請(qǐng)輸入圓的半徑" << endl;cin >> r1;c1.setR(r1);cout << "圓的面積為:" << c1.getS() << endl;return 0; }運(yùn)行結(jié)果:
不難發(fā)現(xiàn)有成員函數(shù)和沒有成員函數(shù)答案是不一樣的,為什么沒有成員函數(shù)的答案會(huì)是一個(gè)亂碼?
畫個(gè)圖來(lái)理解一下。
?
當(dāng)給r賦值并不會(huì)影響area的值,因?yàn)楫?dāng)實(shí)例化類對(duì)象時(shí),就已經(jīng)為類對(duì)象的數(shù)據(jù)成員分配了空間。給r賦值,并不會(huì)影響area中初始化時(shí)的r的隨機(jī)值。相互獨(dú)立。
所以沒有成員函數(shù)和有成員函數(shù)的相差很大。
參考自https://blog.csdn.net/sum_TW/article/details/52108421
?
?
總結(jié)
以上是生活随笔為你收集整理的C++基础04-类基础的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++基础03-C++对c的拓展-函数
- 下一篇: tensorFlow13卷积神经网络发展