C++相关小知识点笔记

//float4个字节 double8个字节 以最大基本类型开辟空间
//若干属性位置顺序影响开辟空间
//int double int 24个字节 double int int 16个字节
//PI类属性 对象属性
//常量声明实现都要加const 只读不写
class Circle
{
private:

float radius;
static const double PI; //常量

public:
Circle(float);
Circle();
//Area构成重载,常成员函数和类函数
float Area() const;//_const指针加以区分
// float Area();

//拷贝构造函数 传属性  1.函数形参或返回值是对象类型   2.一个对象生成另一个对象 触发拷贝构造函数
Circle(const Circle &c)//引用类型,就是指针类型
{
    this->radius = c.radius;
}

static double getPI()  //静态成员变量使用需要静态函数
{
    return PI;
}

};
//static const double PI(3.1415); //静态/类常量

//初始化常量PI\

Circle::Circle()//:PI(3.1415)
{
}

template
class A{
private :
T x;
public:
A::A(T x) :x(x)
{
this->x = x;
}
template
void display() const
{
cout << x << ” “;
}

};

A a(2);
a.display();

const Complex operator+(const Complex &c)const; //左边const定义返回左边值是常量 中间是常对象 最右边常成员函数

//friend ostream & operator<<(ostream &, const Complex &); //友元函数声明类的朋友,打破类的封装性
Complex& operator ++();
Complex operator ++(int);//无论什么类型,加int区分
Complex& operator=(const Complex &);
//friend istream& operator>>(istream &, Complex &);
//输入输出,二元运算符友元函数
//只能用引用,不能拷贝构造和operator=
//因为类的拷贝构造和operator = 是私有的而且没有实现。

int &x=y x就是y int x=y;把y赋值给x
Complex& Complex::operator ++() //一定要返回引用,因为++会改变操作数,而如果是临时对象,操作数据的值不会变
{
++real;
++imaginary;
return *this;
}
Complex Complex::operator ++(int) //后置++
{
//先保存一个*this的临时变量
Complex cT(*this); //cT临时对象,要被析构,所以后置++不能写成引用 相当于 int x,return &x;
//对this自身++
++real;
++imaginary;
return cT;

}
Complex& Complex::operator=(const Complex &c)
{
//地址判断是否是同一个对象
if (this!=&c) //this地址的指针,&c 对象c的地址
real = c.real;
imaginary=c.imaginary;
return *this;
}

//运算符重载
//运算符重载规则,不允许自己定义新的运算符,只能对已有的运算符重载
//不能为基础数据类型重载运算符 int operator+(int x,int y)
//运算符可重载为类的成员函数,也可重载为类的友元函数

class string
{
private:
char*_string;
public:
string(const char * = 0);
~string();
//重载赋值运算符
//重载格式 返回值类型 operator 运算符 (形参表)
string& operator = (const string &);
string& operator = (const char *);
//重载下标运算符
char& operator const;
//双目运算符
//istream& operator >>(istream &,string &);//istream &引用类型,不是对象,不会调用构造函数,效率高
//friend ostream& operator<<(ostream &,const string &);
};
string str1(“hello”);
cout<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值