类和对象(三)
类和对象。const this 指针
复习
函数重载:命名一样,参数的类型或者个数不同
初始化列表:初始化属性,比函数体内赋值形式效率高
*初始化类列表形式:1.const成员变量2.引用类型成员变量3.组合类(一个类的成员变量是另一个类的对象)4.继承
组合类(复合类)*
class p{组合:主要构造函数
public:
p();
p(int x,int y,intrados);
private:
point a;//组合类
int m;
}
p::p(int x,int y,int r):a(x,y),radius(r){}
析构函数:当生命周期结束时,释放空间(回收空间)(构造函数成员变量指针指向堆的空间)
对象销毁时,自动释放空间(如果成员变量没有指针,不需要写析构函数,如果成员变量包含指针,那么指针空间一定分配在堆上,析构函数)
new:在堆上开辟空间
类和对象、const、this 指针
const
//c中的常量
// const int num1=10;//只能读,不能写
// int num2=num1+10;//num1=20;//error
//
// //C中指针 指针常量 常量指针、
// //指针常量:指针的值不能发生改变。。
// //常量指针:指针的值可以改变。。
// int num3=10;
// const int *p1=&num3;//常量
// int const *p=&num3;
// int *const p2=&num3;//指针
//C++中const和函数
//const int func(const int a)const;(三个位置)
//返回值const:修饰返回值,只读,不能写
//int num=func();
//func().print//error
/***形参const:修饰形参a为read_only
函数末尾const:修饰成员变量(函数体内不能修改成员变量的值)
const 成员函数只能调用const 成员函数,不能调用非const 成员函数
非const成员函数,可以调用const 成员函数。也可以调用const成员函数。*/**
Text t;
const Text t1;
const Text &cm1=t ;
Text &cm2=t;
//可以通过t做修改,但是不能通过cm2做修改
//cm2不能调用const的函数。但t可以
const Text &cm3=t1;
//Text &cm4=t1;//error
**//const 对象引用
//const 类型对象可以赋值给const类型引用 ,不能赋值给非const类型引用
//非const类型对象即可以赋值给const类型引用,也可以赋值给非const类型引用**
#include <iostream>
using namespace std;
#include "text.hpp"
#include "Rectangle.hpp"
void func(const Text &t ){
cout<<t.getx()<<endl;//常量引用,常引用(const)对象只能调用const类型成员函数
}
//特别注意:返回值Text,不是Text*,Text&
Text middle(const Text t1,const Text t2){
int x1=t1.getx();
int x2=t2.getx();
int y1=t1.gety();
int y2=t2.gety();
int x=(x1+x2)/2;
int y=(y1+y2)/2;
Text t(x,y);
return t;//当返回局部对象,一定要以返回值的形式
}
//Rectangle:: Rectangle(){}
//Rectangle:: Rectangle(int l,int w){
// length=l;
// width=w;
//}
//void Rectangle:: print(){
// cout<<length<<","<<width<<endl;
//}
//int Rectangle::addRectangle(const Rectangle &t1){
// int x1=t1.length;
// int y1=t1.width;
// int x2=this->length;
// int y2=this->width;
// int x=x1+x2;
// int y=y1+y2;
// return x*y;
//}
//int Rectangle:: rectangleArea()const{
// return length*width;
//}
//Rectangle Rectangle:: bigRectangle(const Rectangle &r1,const Rectangle &r2,const Rectangle &r3){
// int l=r1.length+r2.length+r3.length;
// int w=r1.width+r2.width+r3.width;
// Rectangle r(l,w);
// return r;
//}
//Rectangle Rectangle:: maxRectangle(const Rectangle &r1,const Rectangle &r2,const Rectangle &r3){
// Rectangle r;
// if(r1.rectangleArea()>r2.rectangleArea()){
// if(r3.rectangleArea()>r1.rectangleArea()){
// r=r3;
// }
// else
// r=r1;
// }
// else
// {
// if(r3.rectangleArea()>r2.rectangleArea())
// r=r3;
// else
// r=r2;
// }
// return r;
//}
int main(int argc, const char * argv[]) {
/*C++:对象传值1.值2.指针3.引用
void func(Text t)
viod func(Text *t)
void func(Text &t)
一般情况下使用传引用(防止创建临时对象),为了防止函数内修改参数,所有使用常引用const Text &t
函数以传值形式返回一个对象,会自动创建一个临时对象来保存
函数不能返回一个指向局部变量的指针或引用
*/
const Text t1(3,4);
const Text t2(6,8);
Text t3 = middle(t1,t2);
t3.print();
/*
逻辑拷贝(浅拷贝)
当把一个对象赋值给另一个对象(t1=t2),把t2的成员变量拷贝一份分别赋值给c1的每个成员变量,这样构成浅拷贝
如果成员变量包括指针,因为浅拷贝是把值赋值给另一个变量,这样使得两个对象的指针指向同一块堆空间(对象消耗释放内存,造成2次删除错误)
*/
Text t4(3,6);
Text t5(90,40);
t4=t5;
t4.print();
/*
写一个长方形的类:length,width
写函数
1.通过3个长方形构造一个发的长方形(返回一个长方形)
2.计算两个长方形的面积和
3.三个长方形,得到面积最大的长方形(返回面积最大的长方形)
*/
Rectangle r1(3,4);
Rectangle r2(10,20);
Rectangle r3(90,50);
Rectangle r;
Rectangle c;
c=r.bigRectangle(r1,r2,r3);
c.print();
c=r.maxRectangle(r1, r2,r3);
c.print();
int n=r2.addRectangle(r1);
cout<<"两个长方形的面积和:"<<n<<endl;
return 0;
}
结果:
4,6
90,40
103,74
90,50
两个长方形的面积和:312