C++面向对象类的实例题目一
生活随笔
收集整理的這篇文章主要介紹了
C++面向对象类的实例题目一
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
在一個程序中,實現(xiàn)如下要求:
(1)構(gòu)造函數(shù)重載
(2)成員函數(shù)設(shè)置默認參數(shù)
(3)有一個友元函數(shù)
(4)有一個靜態(tài)函數(shù)
(5)使用不同的構(gòu)造函數(shù)創(chuàng)建不同對象
code:
#include<iostream> using namespace std; class A {public:A(){cout<<"Defalut constructor called."<<endl;//默認構(gòu)造函數(shù)count++; }A(int i){cout<<"constructor1=====>a:"<<i<<endl; //有一個參數(shù)的構(gòu)造函數(shù) a = i;count++; }friend void show(A &a1); //定義了一個友元函數(shù) static void show_num() //定義了一個靜態(tài)函數(shù) {cout<<"number:"<<count<<endl; }void set(int i=0) //定義一個具有默認參數(shù)的成員函數(shù) {a = i;} int a;static int count; }; int A::count = 0; //在類的定義之外初始化靜態(tài)成員變量 void show(A &a1) {cout<<"a:"<<a1.a<<endl; } int main() {A a1,a2(5);show(a1);show(a2);A::show_num(); }輸出: Defalut constructor called. constructor1=====>a:5 a:0 a:5 number:2
轉(zhuǎn)載于:https://www.cnblogs.com/zhezh/p/3773357.html
總結(jié)
以上是生活随笔為你收集整理的C++面向对象类的实例题目一的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百灵欧拓O2O移动广告平台
- 下一篇: MVC之前的那点事儿系列(4):Http