This commit is contained in:
Light-City
2020-04-06 00:57:02 +08:00
parent f97c156cc4
commit a4d828bb4c
120 changed files with 4413 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
常类型的对象必须进行初始化,而且不能被更新。
常引用:被引用的对象不能被更新。
const 类型说明符 &引用名
常对象:必须进行初始化,不能被更新。
类名 const 对象名
常数组:数组元素不能被更新。
类型说明符 const 数组名[大小]...
常指针:指向常量的指针。

View File

@@ -0,0 +1,24 @@
#include<iostream>
using namespace std;
void display(const double& r);
class A
{
public:
A(int i,int j) {x=i; y=j;}
private:
int x,y;
};
int main()
{ double d(9.5);
display(d);
A const a(3,4); //a是常对象不能被更新
system("pause");
return 0;
}
void display(const double& r)
//常引用做形参,在函数中不能更新 r所引用的对象。
{ cout<<r<<endl; }

View File

@@ -0,0 +1,41 @@
#include<iostream>
using namespace std;
class R
{ public:
R(int r1, int r2){R1=r1;R2=r2;}
//const区分成员重载函数
void print();
void print() const;
private:
int R1,R2;
};
/*
常成员函数说明格式:类型说明符 函数名参数表const;
这里const是函数类型的一个组成部分因此在实现部分也要带const关键字。
const关键字可以被用于参与对重载函数的区分
通过常对象只能调用它的常成员函数
*/
void R::print()
{
cout<<"普通调用"<<endl;
cout<<R1<<":"<<R2<<endl;
}
//实例化也需要带上
void R::print() const
{
cout<<"常对象调用"<<endl;
cout<<R1<<";"<<R2<<endl;
}
int main()
{
R a(5,4);
a.print(); //调用void print()
//通过常对象只能调用它的常成员函数
const R b(20,52);
b.print(); //调用void print() const
system("pause");
return 0;
}

View File

@@ -0,0 +1,9 @@
# 友元概念?
友元是C++提供的一种破坏数据封装和数据隐藏的机制。
通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。
可以使用友元函数和友元类。
为了确保数据的完整性,及数据封装与隐藏的原则,建议尽量不使用或少使用友元。
# 如何使用?
友元函数是在类声明中由关键字friend修饰说明的非成员函数在它的函数体中能够通过对象名访问 private 和 protected成员
作用:增加灵活性,使程序员可以在封装和快速性方面做合理选择。
访问对象中的成员必须通过对象名。

View File

@@ -0,0 +1,31 @@
//使用友元函数计算两点间距离
#include <iostream>
#include <cmath>
using namespace std;
class Point{
public:
Point(int x=0,int y=0):X(x),Y(y){}
int GetX(){
return X;
}
int GetY(){
return Y;
}
friend float Distance(Point &a,Point &b);
private:
int X,Y;//私有数据成员
};
//通过将一个模块声明为另一个模块的友元,一个模块能够引用到另一个模块中本是被隐藏的信息。
float Distance(Point &a, Point &b){
double dx = a.X-b.X;
double dy = a.Y-b.Y;
return sqrt(dx*dx+dy*dy);
}
int main()
{
Point p1(3.0,5.0),p2(4.0,6.0);
cout<<"两点距离为:"<<Distance(p1,p2)<<endl;
system("pause");
return 0;
}

View File

@@ -0,0 +1,49 @@
#include<iostream>
using namespace std;
/*
若一个类为另一个类的友元,则此类的所有成员都能访问对方类的私有成员。
声明语法将友元类名在另一个类中使用friend修饰说明。
*/
/*
如果声明B类是A类的友元B类的成员函数就可以访问A类的私有和保护数据
但A类的成员函数却不能访问B类的私有、保护数据。
*/
class A{
friend class B;
public:
void Display(){
cout<<x<<endl;
}
private:
int x;
};
class B
{ public:
void Set(int i);
void Display();
private:
A a;
};
void B::Set(int i)
{
a.x=i;
}
void B::Display()
{
a.Display();
}
int main(int argc, char const *argv[])
{
B b;
b.Set(10);
b.Display();
system("pause");
return 0;
}
/*
如果声明B类是A类的友元B类的成员函数就可以访问A类的私有和保护数据但A类的成员函数却不能访问B类的私有、保护数据
*/

View File

@@ -0,0 +1,47 @@
#include<iostream>
#include"clock.h"
using namespace std;
Clock::Clock(int NewH,int NewM,int NewS)
{
this->Hour=NewH;
this->Minute=NewM;
this->Second=NewS;
}
Clock::Clock(Clock &c)
{
this->Hour=c.Hour;
this->Minute=c.Minute;
this->Second=c.Second;
}
void Clock::SetTime(int NewH,int NewM,int NewS)
{
//加不加this指针都一样
this->Hour=NewH;
this->Minute=NewM;
this->Second=NewS;
}
void Clock::ShowTime()
{
cout<<this->Hour<<endl;
cout<<this->Minute<<endl;
cout<<this->Second<<endl;
}
//析构函数
Clock::~Clock()
{
}
int main(int argc, char const *argv[])
{
Clock c(0,0,0);
c.SetTime(10,20,30);
c.ShowTime();
//拷贝构造函数调用
Clock c1(c);
c1.ShowTime();
c1.SetTime(90,98,99);
c1.ShowTime();
system("pause");
return 0;
}

View File

@@ -0,0 +1,26 @@
#ifndef CLOCK
# define CLOCK
class Clock
{
public:
Clock(int NewH,int NewM,int NewS);
Clock(Clock &c);//拷贝构造函数,如果不加,编译器会自动生成一个拷贝构造函数,因此加不加都可以直接使用。
void SetTime(int NewH,int NewM,int NewS);
void ShowTime();
~Clock();//析构函数,编译器会自动产生一个默认的析构函数。
private:
int Hour,Minute,Second;
};
#endif
/*
#ifndef 标识符
程序段1
#else
程序段2
#endif
如果“标识符”未被定义过则编译程序段1否则编译程序段2。
*/

View File

@@ -0,0 +1,25 @@
#include <iostream>
using namespace std;
class Time{
private:
int hh,mm,ss;
public:
Time(int h=0,int m=0,int s=0):hh(h),mm(m),ss(s){}
void operator()(int h,int m,int s) {
hh=h;
mm=m;
ss=s;
}
void ShowTime(){
cout<<hh<<":"<<mm<<":"<<ss<<endl;
}
};
int main(){
Time t1(12,10,11);
t1.ShowTime();
t1.operator()(23,20,34);
t1.ShowTime();
t1(10,10,10);
t1.ShowTime();
system("pause");
}

View File

@@ -0,0 +1,45 @@
/*
设计一个时钟类,能够记录时、分、秒,重载它的++运算符,每执行一次++运算加时1秒但要使计时过程能够自动进位。
*/
#include<iostream>
using namespace std;
class Time{
public:
Time(int h=0,int m=0,int s=0){
hour = h;
minute = m;
second = s;
}
void operator++();
void showTime(){
cout<<"当前时间为:"<<hour<<":"<<minute<<":"<<second<<endl;
}
private:
int hour,minute,second;
};
void Time::operator++(){
++second;
if(second=60){
second=0;
++minute;
if(minute==60){
minute=0;
hour++;
if(hour==24){
hour=0;
}
}
}
}
int main(int argc, char const *argv[])
{
Time t(23,59,59);
++t;
t.showTime();
system("pause");
return 0;
}