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;

}

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