日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++使用类和对象(谭浩强9.8-9.14)

發布時間:2023/12/20 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++使用类和对象(谭浩强9.8-9.14) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

例9.8 對象的引用

#include <iostream> using namespace std; class Time { public:Time(int, int, int);//聲明構造函數int hour;int minute;int sec; }; Time::Time(int h, int m, int s)//定義構造函數 {hour = h;minute = m;sec = s; } void fun(Time &t)//形參t是Time類對象的引用 {t.hour = 18; } int main() {Time t1(10, 13, 56);fun(t1); //實參是Time類對象,可以通過引用來修改實參t1的值cout << t1.hour << endl;//輸出t1.hour的值為18return 0; }

程序執行結果如圖:

例9.9 對象的賦值和復制

#include <iostream> using namespace std; class Box { public:Box(int=10, int=10, int=10);//聲明帶默認參數的構造函數int volumn(); private:int height;int width;int length; }; Box::Box(int h, int w, int l)//在類外定義帶參數的構造函數 {height = h;width = w;length = l; } int Box::volumn()//定義成員函數 {return(height*width*length); } int main() {Box box1(15, 30, 25),box2;//建立對象box1,box2,并指定box1的高寬長的值cout << "The volumn of box1 is" << box1.volumn() << endl;box2 = box1;//將box1的值賦給box2cout << "The volumn of box2 is" << box2.volumn() << endl; }

執行結果如圖:

例10 靜態數據成員(可以通過對象名引用,也可以通過類名引用)

#include <iostream> using namespace std; class Box { public: Box(int, int);//聲明帶參數的構造函數 int volumn(); static int height; int width; int length; }; Box::Box(int w, int l)//在類外定義帶參數的構造函數 {width = w;length = l; } int Box::volumn()//定義成員函數 {return(height*width*length); } int Box::height = 10; int main() {Box a(15, 20),b(20,30);//建立對象a,b,并指定a,b的寬長的值cout << a.height << endl;//通過對象名a引用靜態數據成員heightcout << b.height << endl;//通過對象名b引用靜態數據成員heightcout << Box::height << endl;//通過類名引用靜態數據成員heightcout << "The volumn of box1 is" << a.volumn() << endl;return 0; }

程序執行效果如圖:

例9.11靜態成員函數:統計學生平均成績

#include <iostream> using namespace std; class Student { public:Student(int n, int a, float s) :num(n), age(a), score(s){}void total();//聲明成員函數static float average();//聲明靜態成員函數 private:int num;int age;float score;static float sum;//靜態成員數據static int count;//靜態成員數據 }; void Student::total()//定義非靜態成員函數 {sum += score;count++; } float Student::average()//定義靜態成員函數 {return(sum / count); } float Student::sum = 0;//靜態數據成員在類外進行初始化 int Student::count = 0;//靜態數據成員在類外進行初始化 int main() {Student stud[3] = {Student(1001, 18, 70),Student(1002, 19, 78),Student(1005, 20, 98)};//定義對象數組并初始化int n;cout << "please input the number of students:";cin >> n;for (int i = 0; i < n; i++)stud[i].total();//調用n次total函數cout << "the average score of" << n << "student is" <<Student::average() << endl;return 0; }

程序執行效果如圖:

例9.13:友元成員函數:

#include <iostream> using namespace std; class Date;//對Date類的提前引用聲明 class Time//聲明Time類 { public:Time(int, int, int);//聲明構造函數void display(Date&);//display是成員函數,形參是Date類對象的引用//即該函數是Date的友元函數可以訪問Date類的成員 private:int hour;int minute;int sec; }; class Date//聲明Date類 { public:Date(int, int, int);//聲明構造函數friend void Time:: display(Date&);//聲明Time類中的display函數為本類的友元函數 private:int month;int day;int year; }; Time::Time(int h, int m, int s)//定義Time類構造函數 {hour = h;minute = m;sec = s; } void Time::display(Date &d)//形參d是Date類對象的引用 {cout << d.month << "/" << d.day << "/" << d.year << "/" << endl;//引用Date類對象中的私有數據cout << hour << ":" << minute << ":" << sec << endl; }//引用Date類對象中的私有數據 Date::Date(int m, int d, int y)//類Date的構造函數 {month = m;day = d;year = y; } int main() {Time t1(10, 13, 56);Date d1(12,25,2004); //實參是Time類對象,可以通過引用來修改實參t1的值t1.display(d1);return 0; }

程序執行結果如圖:

9.14類模板的使用:

#include <iostream> using namespace std; template<class numtype>//生命類模板,虛擬類型名為numtype class Compare//類模板為compare { public:Compare(numtype a,numtype b)//定義構造函數{x = a; y = b;}numtype max()//函數類型暫定為numtype{return(x > y) ? x : y;}numtype min(){return(x < y) ? x : y;} private:numtype x, y; }; int main() {Compare<int>cmp1(3, 7);//定義對象cop1,用于兩個整數的比較cout << cmp1.max() << "is the Maximun of two integer numbers." << endl;cout << cmp1.min() << "is the Minimun of two integer numbers." << endl << endl;Compare<float>cmp2(45.78, 93.6);cout << cmp2.max() << "is the Maximun of two float numbers." << endl;cout << cmp2.min() << "is the Minimun of two float numbers." << endl << endl;Compare<char>cmp3('a','A');cout << cmp3.max() << "is the Maximun of two characters." << endl;cout << cmp3.min() << "is the Minimun of two characters." << endl << endl;return 0; }

執行結果如圖:

總結

以上是生活随笔為你收集整理的C++使用类和对象(谭浩强9.8-9.14)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。