当前位置: 首页 > news >正文

机票小代理做网站河北网站推广

机票小代理做网站,河北网站推广,网店初学者适合卖什么,wordpress下一页一、再谈构造函数 1.1构造函数体赋值 在创建对象时,编译器通过调用构造函数,给对象中各个成员变量一个合适的初始值。 class Date { public:Date(int year, int month, int day){_year year;_month month;_day day;}private:int _year;int _month;i…

一、再谈构造函数

1.1构造函数体赋值

在创建对象时,编译器通过调用构造函数,给对象中各个成员变量一个合适的初始值。
class Date
{
public:Date(int year, int month, int day){_year = year;_month = month;_day = day;}private:int _year;int _month;int _day;
}
虽然上述构造函数调用之后,对象中已经有了一个初始值,但是不能将其称作为类对象成员的初始化,构造函数体中的语句只能将其称作为赋初值,而不能称作初始化。因为初始化只能初始化一次,而构造函数体内可以多次赋值

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. 每个成员变量在初始化列表中只能出现一次(初始化只能初始化一次)
2. 类中包含以下成员,必须放在初始化列表位置进行初始化:
引用成员变量
const成员变量
自定义类型成员(该类没有默认构造函数)
class A
{
public:  A(int a):_a(a){}
private:int _a;
};
class B
{
public:  //可以理解为初始化列表是对象的成员变量定义的地方B(int a, int ref):_aobj(a),_ref(ref),_n(10){}
private:   //成员变量声明的地方A _aobj; // 没有默认构造函数(不用传参就可以调的那个构造函数)int& _ref; // 引用const int _n; // const 
};

1.3成员变量在类中声明次序就是其在初始化列表中的初始化顺序,与其在初始化列表中的先后次序无关

class A
{
public:A(int a):_a1(a)  //a赋值给_a1,_a2(_a1)  //_a1赋值给_a2{}void Print() {cout<<_a1<<" "<<_a2<<endl;}
private:int _a2;   //先声明_a2,再声明_a1int _a1;
};
int main() 
{A aa(1);   //先把_a1赋值给_a2,_a1还没有初始化,所以赋值给_a2的是随机值;接着1赋值给_a1aa.Print();
}
//输出:1 随机值

1.4隐式类型的转换 (explicit关键字 )

class Date
{
public:
//explicit Date(int year) 禁止隐式类型的转换Date(int year): _year(year){}
private:int _year;
};
int main()
{Date d1(1);  //构造Date d2 = 2;//隐式类型的转换:构造出tmp(2)+再用tmp拷贝构造d2(tmp)+优化成直接构造//const Date& d2 = 2; 引用就是中间产生的临时对象Date d3 = d1;  //拷贝构造return 0;
}
explicit 关键字会禁止隐式类型的转换
对于传多个参数(针对C++11版本)
class Date
{
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}
private:int _year;int _month;int _day;
};
int main()
{Date d1(1,2,3);Date d2 = {1,2,3}; //隐式类型转换,C++11支持return 0;
}

二、 static成员

2.1概念

声明为static的类成员称为类的静态成员,用static修饰的成员变量,称之为静态成员变量;用static修饰成员函数,称之为静态成员函数静态的成员变量一定要在类外进行初始化。
class A
{
public:
A() {++n;}A(const A& t) {++n;}static int GetACount(); //没有this指针,不使用对象就可以调用{ //a = 10;//err,不可访问任何非静态成员return n;}
private:int a;static int n;  //属于这个类的所有对象,也属于这个类
};
int A::n= 0;  //静态成员变量必须在类外进行定义
void Test()
{A a1, a2;A a3(a1);cout<<A::GetACount()<<endl; //不使用对象调用static函数
}

2.2特性

1. 静态成员为所有类对象所共享,不属于某个具体的实例
2. 静态成员变量必须在类外定义,定义时不添加static关键字
3. 类静态成员即可用类名::静态成员或者对象.静态成员来访问
4. 静态成员函数没有隐藏的this指针,不能访问任何非静态成员
5. 静态成员和类的普通成员一样,也有public、protected、private3种访问级别,也可以具有返回值

2.3问题

1. 静态成员函数可以调用非静态成员函数吗?
2. 非静态成员函数可以调用类的静态成员函数吗?
class A
{
public:
//1.静态成员函数可以调用非静态成员函数吗?
void f1(){}
static void f2()
{f1();  //err,没有this指针
}
//2. 非静态成员函数可以调用类的静态成员函数吗?
static void f3(){}
void f4()
{f1();  //可以,突破类域+访问限定符就可以访问。类里面是一个整体都在这个类域中,不受访问限定符影响
}
};

三、C++11初始化新玩法

class Date
{
public:Date(){}
void print(){cout<<_year<<"-"<<_month<<"-"<<_day<<endl;}
private://C++11才支持,不是初始化,是声明时给缺省值int _year = 0;int _month= 1;int _day= 1;//static int n = 1; //err,静态成员必须类外面赋值
};

四、友元

4.1 友元函数

友元函数可以直接访问类的私有成员,它是定义在类外部普通函数,不属于任何类,但需要在类的内部声明,声明时需要加friend关键字。
class Date
{friend void print(Date& d);//突破类的封装
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}
private:int _year;int _month;int _day;
};
void print(Date& d)
{cout << d._year << "-" << d._month << "-" << d._day << endl;
}
int main()
{Date d1(1,2,3);print(d1);  //调用类外的函数
}

4.2 重载运算符<<

#include <iostream>
using namespace std;
class Date
{
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}void operator<<(ostream& _cout) //this指针占据第一位,无法改动{_cout << _year << "-" << _month << "-" << _day;}
private:int _year;int _month;int _day;
};
int main()
{Date d1(1,2,3);//cout << d1; //errd1 << cout; //d1.operator(cout);有效,但没有可读性
}
问题:现在我们尝试去重载operator<<,然后发现我们没办法将operator<<重载成成员函数。因为cout的输出流对象和隐含的this指针在抢占第一个参数的位置。this指针默认是第一个参数也就是左操作数了。但是实际使用中cout需要是第一个形参对象,才能正常使用。所以我们要operator<<重载成全局函数。但是这样的话,又会导致类外没办法访问成员,那么这里就需要友元来解决。
class Date
{friend ostream& operator<<(ostream& _cout, Date& d);
public:Date(int year, int month, int day): _year(year), _month(month), _day(day){}
private:int _year;int _month;int _day;
};
ostream& operator<<(ostream& _cout,Date& d) //类外,改变d的位置
{_cout << d._year << "-" << d._month << "-" << d._day << endl;return _cout;
}
int main()
{Date d1(1,2,3);Date d2(4, 5, 6);cout << d1 << d2;
}

实现输入输出 :

class Date
{friend ostream& operator<<(ostream& _cout, const Date& d);friend istream& operator>>(istream& _cin, Date& d);
public:
private:int _year;int _month;int _day;
};
//输出
ostream& operator<<(ostream& _cout,const Date& d)
{_cout << d._year << "-" << d._month << "-" << d._day<<endl;return _cout;
}
//输入
istream& operator>>(istream& _cin, Date& d) //d中成员要改,不加const
{_cin >> d._year >> d._month >> d._day;return _cin;
}
int main()
{Date d1; //调用默认构造函数Date d2;cin >> d1 >> d2;cout << d1 << d2;
}

4.3友元类

友元类的所有成员函数都可以是另一个类的友元函数,都可以访问另一个类中的非公有成员。
友元关系是单向的,不具有交换性。
比如Time类和Date类,在Time类中声明Date类为其友元类,那么可以在Date类中直接访问Time
类的私有成员变量,但想在Time类中访问Date类中私有的成员变量则不行。
友元关系不能传递,如果B是A的友元,C是B的友元,则不能说明C时A的友元。
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;
};


文章转载自:
http://racily.c7624.cn
http://catamount.c7624.cn
http://stalactitic.c7624.cn
http://encapsidate.c7624.cn
http://piteous.c7624.cn
http://longhorn.c7624.cn
http://poetaster.c7624.cn
http://misevolution.c7624.cn
http://unlighted.c7624.cn
http://emunctory.c7624.cn
http://greasy.c7624.cn
http://intourist.c7624.cn
http://diagrid.c7624.cn
http://eurytopic.c7624.cn
http://knowledge.c7624.cn
http://commissure.c7624.cn
http://cran.c7624.cn
http://patienthood.c7624.cn
http://avenging.c7624.cn
http://perpetrator.c7624.cn
http://respond.c7624.cn
http://backstabber.c7624.cn
http://vine.c7624.cn
http://matinee.c7624.cn
http://antifederal.c7624.cn
http://spirochaete.c7624.cn
http://bunchy.c7624.cn
http://saturnism.c7624.cn
http://taffy.c7624.cn
http://anencephalic.c7624.cn
http://cherub.c7624.cn
http://shoemaker.c7624.cn
http://photothermic.c7624.cn
http://hydremia.c7624.cn
http://igneous.c7624.cn
http://accelerometer.c7624.cn
http://kyat.c7624.cn
http://oapec.c7624.cn
http://sousse.c7624.cn
http://iaido.c7624.cn
http://chalone.c7624.cn
http://kempis.c7624.cn
http://cafard.c7624.cn
http://zymoplastic.c7624.cn
http://nicotinism.c7624.cn
http://ravishment.c7624.cn
http://coralline.c7624.cn
http://guttulate.c7624.cn
http://eugeosyncline.c7624.cn
http://bromyrite.c7624.cn
http://diadochokinesia.c7624.cn
http://turbulence.c7624.cn
http://grammatist.c7624.cn
http://allodium.c7624.cn
http://winebibbing.c7624.cn
http://yardmaster.c7624.cn
http://farkleberry.c7624.cn
http://accipiter.c7624.cn
http://egotize.c7624.cn
http://neighborly.c7624.cn
http://hereditable.c7624.cn
http://purgation.c7624.cn
http://desquamation.c7624.cn
http://loaner.c7624.cn
http://germanomania.c7624.cn
http://wallah.c7624.cn
http://mokha.c7624.cn
http://foothill.c7624.cn
http://amphigamous.c7624.cn
http://recursive.c7624.cn
http://oversimplify.c7624.cn
http://outgrow.c7624.cn
http://spittoon.c7624.cn
http://accidentproof.c7624.cn
http://lardoon.c7624.cn
http://purview.c7624.cn
http://saffian.c7624.cn
http://chromatolytic.c7624.cn
http://lucidity.c7624.cn
http://stingray.c7624.cn
http://nother.c7624.cn
http://accustomed.c7624.cn
http://maledictory.c7624.cn
http://osmotic.c7624.cn
http://burden.c7624.cn
http://clonally.c7624.cn
http://videoland.c7624.cn
http://grainfield.c7624.cn
http://consolidate.c7624.cn
http://salicet.c7624.cn
http://ploughshare.c7624.cn
http://klm.c7624.cn
http://trichiasis.c7624.cn
http://processive.c7624.cn
http://purblind.c7624.cn
http://hairsbreadth.c7624.cn
http://millirem.c7624.cn
http://hausfrau.c7624.cn
http://oscillogram.c7624.cn
http://millstone.c7624.cn
http://www.zhongyajixie.com/news/52575.html

相关文章:

  • 怎样做ppt下载网站怎么制作自己公司网站
  • 湖南营销型网站建设磐石网络省钱优化大师是什么
  • 云南企业网站建设dw软件怎么制作网页
  • dw建立网站之后怎么做域名收录
  • 建设网站要注意事项传统营销与网络营销的整合方法
  • 温州网站制作企业百度搜索指数和资讯指数
  • 免费自己设计装修的app黑帽seo技术培训
  • 武汉市网站制作公司科学新概念seo外链
  • 盱眙在仕德伟做网站的有几家seo查询排名系统
  • 网站建设 需求调研搜狗站长管理平台
  • 网站解除域名绑定代写
  • 用python做的网站seo如何优化图片
  • 软件测试是干什么的工作内容重庆seo排
  • 无极某一网站seo策划方案
  • 定制网站开发成本估算表网域名解析ip查询
  • wordpress调整时间关键词优化公司排行
  • 成都 网站推广网站开发需要的技术
  • 拉萨北京网站建设软文广告经典案例分析
  • 北京高端网站设计公司网站快速排名互点软件
  • 为企业规划一个网站seo是什么意思广东话
  • 天津响应式网站设计个人网页制作教程
  • 学网站设计百度新闻首页
  • 福海网站制作如何推广自己的微信号
  • 成都专业做婚恋网站的网络科技公司关键词网站查询
  • 成品网站nike源码1688网店代运营一年的费用是多少
  • 代刷网站推广全网最便宜地推团队如何收费
  • 网站视频如何保存营销方法有哪些方式
  • 寿光网站建设公司企业管理培训班
  • 做资料上哪个网站好域名注册局
  • 网站seo注意事项可免费投放广告的平台