CPP類與對象下

1. 再談構造函數

#include<iostream>

using namespace std;

class Date{
public:
    Date(int year){  //構造函數用來初始變量
        _year = year;
    }

private:
    int _year;
};


int main(){
    Date d1(2019);
    //自動調用Date(int year ) 函數
    Date d2 = 2018;
    //也可以自動調用Date(int year ) 函數
    return 0;

}

1.2 初始化列表

  • 存在於函數名與函數體之間
  • 初始化列表:以一個冒號開始,接着是一個以逗號分隔的數據成員列表,每個"成員變量"後面跟一個放在括號中的初始值或表達式。
  • 默認構造函數:無參或者全缺省
如下例
class Date
{
public:
 Date(int year, int month, int day)
 : _year(year)
 , _month(month)
 , _day(day)
 {}
 
private:
 int _year;
 int _month;
 int _day;
};

1.3關鍵字explicit

構造函數不僅可以構造與初始化對象,對於單個參數的構造函數,還具有類型轉換的作用

class Date
{
public:
	Date(int year)
		:_year(year)
	{}
	/*explicit Date(int year)
		:_year(year)
	{}*/

private:
	int _year;
	int _month;
	int _day;
};
void TestDate()
{
	Date d1(2018);

	// 用一個整形變量給日期類型對象賦值
	// 實際編譯器背後會用2019構造一個無名對象,最後用無名對象給d1對象進行賦值
	d1 = 2019;//使用explicit時這種賦值也可以使用,阻止單三形式的隱式轉換
}
int main(){
	TestDate();
	system("pause");
	return 0;
}

上述代碼可讀性不是很好,用explicit修飾構造函數,將會禁止單參構造函數的隱式轉換。

​ 2.關鍵字 static

2.1 概念

聲明爲static的類成員稱爲類的靜態成員,用static修飾的成員變量,稱之爲靜態成員變量;
用static修飾的成員函數,稱之爲靜態成員函數。靜態的成員變量一定要在類外進行初始化
對於靜態成員函數不能訪問它的成員變量,因爲他沒有this指針
class Date{
    public:
    Date(){
        
    }
Date (const Date&d1)
};
int main(){
    Date d;
    cout<<Date::count<<endl;
    cout<<d.count<<endl; //公有成員的訪問方式
    return 0;
}

靜態函數需要用類名+作用限定符去調用或者用對象去調用(. 操作符);

#include<iostream>

#include<Windows.h>

//此情況爲static定義的函數爲公有的
using namespace std;
class Date{
public:
  Date(int year){
      _count++;
      _year = year;
      cout << "Date(int)" << endl;

  }
  Date(const Date& d1){

      _count++;
      _year = d1._year;
      cout << "Dtae(const Date& d1)" << endl;

  }

//private: //將static定義爲私有成員時就不能用d1._count 的方式訪問了。
  int _year;
  static int _count; 
  //static定義的變量在類外面進行初始化
};

int Date::_count = 0; //static定義的變量在類外初始化格式爲"類型+作用域"

Date fun1(Date d1){
  return d1;
}

int main(){
  Date d1(2019);
  Date d2(2015);
  Date d3(2015);//到此爲止已經調用了3次構造函數
  fun1(d1);    //
  cout << d1._count<< endl;
  cout << &d1._count << endl;
  cout << &d2._count << endl;
  cout << &d3._count << endl;
  system("pause");
  return 0;

}
#include<iostream>

#include<Windows.h>

//此情況爲static定義的函數爲私有的
using namespace std;

class Date{

public:

  Date(int year){
      _count++;
      _year = year;
      cout << "Date(int)" << endl;

  }

  Date(const Date& d1){

      _count++;
     _year = d1._year;
     cout << "Dtae(const Date& d1)" << endl;
  }

  int getCount(){
      //作用是將count 的值傳到需要調用count的地方
      return _count;

  }



//改爲靜態函數後,出現錯誤,靜態函數不能調用非靜態函數     
 // static int getCount(){
 //                    //
 //       return _count;
 //  }
private:
  int _year;
  static int _count;

};

int Date::_count = 0;

//static定義的變量在類外初始化

Date fun1(Date d1){             //fun(Date *this)
                      //非靜態函數可以調用非靜態函數
  return d1;
}



int main(){
  Date d1(2019);
  Date d2(2015);
  Date d3(2015);
  fun1(d1);
  cout << d1.getCount() << endl;
  system("pause");
  return 0;
}

2.2 特性

靜態成員爲所有類對象所共享,不屬於某個具體的實例

靜態成員變量必須在類外定義,定義時不添加static關鍵字

類靜態成員即可用類名::靜態成員或者對象.靜態成員來訪問

靜態成員函數沒有隱藏的this指針,不能訪問任何非靜態成員

靜態成員和類的普通成員一樣,也有public、protected、private3種訪問級別,也可以具有返回值,const修飾符等參數

3.c++ 11的成員初始化新玩法

c++11 支持非靜態的成員變量在聲明時直接進行初始化
#include<iostream>
#include<windows.h>

using namespace std;

class Date{
public:

	Date(int year,int month,int day)//構造函數
	:_year(year)      //初始化可以放在初始化列表哪裏構造函數賦值實現                               //也可以在聲明時直接賦值
	,_month(month)
	,_day(day)
        //既在這裏完成了函數參數的初始化
	{
		cout << _year <<"-"<< _month<<"-" << _day << endl;
        //輸出的結果即爲傳參時傳遞上來的值
	}
private:
	int _year;
	int _month;
	int _day;

};
int main(){
	Date d1(2019,3,30);
	system("pause");
	return 0;
}

4. 友元

友元分爲:友元函數和友元類
友元提供了一種突破封裝的方式,有時提供了便利。但是友元會增加耦合度,破壞了封裝,所以友元不宜多用

4.1友元函數

友元函數可以直接訪問類的私有成員,它是定義在類外部的普通函數,不屬於任何類,但需要在類的內部聲明,聲明時需要加friend關鍵字

class Date
{
public:
 Date(int year, int month, int day)
 : _year(year)
 , _month(month)
 , _day(day)
 {}
 
 ostream& operator<<(ostream& _cout)
 {
 _cout<<d._year<<"-"<<d._month<<"-"<<d._day;
 return _cout;
 }
 
prvate:
 int _year;
 int _month;
 int _day
};
int main()
{
 Date d(2017, 12, 24);
 d<<cout;
 return 0;
}
class Date
{
friend ostream& operator<<(ostream& _cout, const Date& d);
friend istream& operator>>(istream& _cin, const Date& d);
public:
 Date(int year, int month, int day)
 : _year(year)
     , _month(month)
 , _day(day)
 {}
 
prvate:
 int _year;
 int _month;
 int _day
};
ostream& operator<<(ostream& _cout, const Date& d)
{
 _cout<<d._year<<"-"<<d._month<<"-"<<d._day;
 
 return _cout;
}
istream& operator>>(istream& _cin, const Date& d)
{
 _cin>>d._year;
 _cin>>d._month;
 _cin>>d._day;
 
 return _cin;
}
int main()
{
 Date d;
 cin>>d;
 cout<<d<<endl;
 return 0;
}

說明:
友元函數可訪問類的私有成員,但不是類的成員函數
友元函數不能用const修飾
友元函數可以在類定義的任何地方聲明,不受類訪問限定符限制
一個函數可以是多個類的友元函數
友元函數的調用與普通函數的調用和原理相同

4.2 友元類

友元類的所有成員函數都可以是另一個類的友元函數,都可以訪問另一個類中的非公有成員。

友元關係是單向的,不具有交換性。

比如下述Time類和Date類,在Time類中聲明Date類爲其友元類,那麼可以在Date類中直接訪問Time

類的私有成員變量,但想在Time類中訪問Date類中私有的成員變量則不行。

友元關係不能傳遞.如果B是A的友元,C是B的友元,則不能說明C時A的友元

class Date{

   friend class Time             //Time 可以訪問 Date 對象的所有成員

};



class Time{

   friend class Date            //Date 可以訪問 Time 對象的所以成員

};

友元關係不能傳遞

class Date; // 前置聲明
class Time
{
 friend class Date; // 聲明日期類爲時間類的友元類,則在日期類中就直接訪問Time類中的私有成員變量
public:
 Time(int hour, int minute, int second)
 : _hour(hour)
 , _minute(minute)
 , _second(second)
 {}
 
private:
 int _hour;
 int _minute;
 int _second;
};
class Date
{
public:
 Date(int year = 1900, int month = 1, int day = 1)
 : _year(year)
 , _month(month)
 , _day(day)
 {}
 
 void SetTimeOfDate(int hour, int minute, int second)
 {
 // 直接訪問時間類私有的成員變量
 _t._hour = hour;
 _t._minute = minute;
 _t.second = second;
 }
 
private:
 int _year;
 int _month;
 int _day;
 Time _t;
};

5. 內部類

特性:

內部類可以定義在外部類的public、protected、private都是可以的。
注意內部類可以直接訪問外部類中的static、枚舉成員,不需要外部類的對象/類名。
sizeof(外部類)=外部類,和內部類沒有任何關係。
#include<iostream>
#include<windows.h>
using namespace std;
class A{
public:
	//B是A的內部類,相當於B是A的友元類
	class B{

	public:
		void display(A& a){
			cout << a._a << endl;
			//外部類的普通的私有成員只能通過 對象. 的方式訪問
			cout << _s << endl;          //直接訪問
			cout << a._s << endl;        //對象. 的方式訪問
			//外部類的靜態的私有成員可以通過 對象. 的方式訪問 也可以直接訪問
	
		}
	private:
		int _b;

		void Display(B& b){
			//外部類對於內部類沒有任何優越的訪問權限,不能訪問內部的私有成(_b)
			cout << b._b << endl;
		}

	};
private:
	int _a=10;//非靜態變可以直接在定義的時候初始化
	static int _s;//靜態變量必須在類外面進行初始化

};

int A::_s = 0;
int main(){
	 A::B b;
	 A a;
	 b.display(a);
	system("pause");
	return 0;
}
#include<iostream>
#include<windows.h>
using namespace std;
class A{
	//B是A的內部類,相當於B是A的友元類
	public: //若不寫作用限定符則會出現訪問不了B的現象
	class B{
	public :
		void Print(A& a){
			cout << a._a << endl;
			//外部類的普通成員必須通過“對象 . ”的方式去訪問
			cout << _s << endl; 
			//外部類的靜態成員可以通過“對象 .”的方式去訪問
			// 也可以直接去訪問
			cout << a._s << endl;
		}
	private:
		int b;
	};
	void Print(B& b){
        //外部類對於內部類沒有任何優越的訪問權限,不能訪問
		//內部類的私有成員
		//cout << b._b << endl;
	}
private:
	int _a=0;
	static int _s;
};
int A::_s = 1;
int main(){
	A::B a;
	a.Print(A());
	system("pause");
	return 0;
}

題目

2.求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等關鍵字及條件判斷語句(A?B:C).
#include<iostream>
#include<windows.h>

using namespace std;

class A{
public:
   class Sum{
    public:
        Sum(){

            _sum += _i;

            ++_i;

        }

    };

    int Sum_solution(int n){
        _sum=0;    //重新初始化,防止多次調用造成多加的情況
        _i=1;
        Sum arr[5];
        return _sum;
    }

private:
    static int _sum;
    static int _i;
};

int A::_sum = 0;

int A::_i = 1;

int main(){

    A a;

    cout << a.Sum_solution(0) << endl;

    system("pause");

    return 0;

}

輸入年月日,判斷改天是今年的多少天
#include<iostream>

#include<windows.h>

using namespace std;

int main(){

    int days[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

    int year, month, day;

    cin >> year>> month>> day;

    int total = days[month - 1] + day;

    if (month>2){

        if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)){

            total += 1;

        }

    }

    cout << total << endl;

    system("pause");

    return 0;

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章