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