Day 66 构造函数, 复制构造函数,析构函数 ,匿名对象(临时对象)

1.什么是抽象:面向对象方法中的抽象,是指对具体问题(对象)进行概括,抽出一类对象的公共性质,并加以描述的过程。

2.封装就是将抽象得到的数据(属性)和行为相结合,形成一个有机的整体;也就是将数据与处理数据的函数相结合。

3.继承在原来类的基础上,添加新的属性和行为,时对于对象具有更多的属性和行为。

4.多态,多态性是指一段数据可以处理多种数据类型对象的能力;重载函数就是实现多态的一种手段。
多态可以分为:强制多态,重载多态,类型参数化多态, 包含性多态;四种形式来实现。
包含性多态: 一开始程序不知道变量的类型,比如游戏中选人物角色, 一开始程序就已经运行了,但在选择人物角色之前,并不用户要选择那个校色,
要选择的角色,其实也就是一个对象,。。。。。

5.函数重载或者对以有函数初始化时, 如果要默认一些初始化,则初始化必须放在声明处。

6.类如果用class修饰则里面的属性和行为默认都是私有的(private),如果有struct 关键字修饰,则里面的属性和行为默认都是公有的(public)。
7.构造函数,复制构造函数,析构函数。
复制构造函数,参数为类的常引用。
匿名对象,临时对象。
只有构造函数可以使用初始化列表。

class MyComplex
{

public:

 MyComplex(double r,double i)
     :real(r),imag(i)
 {
     cout << "constructor" << endl;
 }

 MyComplex(const MyComplex &other)
     :real(other.real),imag(other.imag)
 {
     cout <<"copy constructor" <<endl;
 }

void show()
{
   cout<< real<< "+" << imag<< "i" << endl;
}

~MyComplex()
{
    cout<< "disconstruct " <<endl;
}
private:
double real;
double imag;

};

void foo(MyComplex c)
{

c.show();

}

MyComplex bu()
{
//这块会复制构造函数,将会有一个匿名对象被创建(临时对象),将返回,bu函数名将指向这个对象。
MyComplex ret(3,4);
return ret;
}

int main()
{
MyComplex c(1,2);
c.show();
foo©;
bu().show();

cout << "hello world " << endl;

}


#include
#include “circle.h”
using namespace std;

class Clock
{
public:

//1.传参
//2.按照数据成员声明的顺序,初始化列表给出的形式,为数据成员开辟内存空间
//3.执行构造函数函数体
explicit Clock(int h = 0, int m = 0, int s = 0)
    :  hour(h),minute(m), second(s)
{
    cout << "Clock(int, int, int)" << endl;
}

//复制构造函数的参数为本类的常引用
Clock(const Clock &other)
    : hour(other.hour), minute(other.minute), second(other.second)
{
    cout << "Clcok(&)" << endl;
}

//1.执行析构函数函数体
//2.按照数据成员生命顺序的逆顺序依次销毁数据成员
~Clock()
{
    cout << "~Clock()" << endl;
}

void showTime()
{
    cout << hour << ":" << this->minute << ":" << second << endl;
}

void setTime(int h,int m,int s)
{
    hour = h;
    minute = m;
    second = s;
}

private:
int hour;
int minute;
int second;
};

void foo(Clock ccc)
{
ccc.showTime();
}

Clock bar()
{
Clock ret(11,22,33);
return ret;
}

#include “mycomplex.h”

//MyComplex add(const MyComplex &c1,const MyComplex &c2)
//{
// MyComplex ret(c1.real + c2.real , c1.imag + c2.imag);
// return ret;
//}

int main()
{

MyComplex m1(1.1,2.2);
m1.show();
MyComplex m2(3.3, 4.4);
m2.show();

m1.add(m2).show();


return 0;

}



#ifndef MYCOMPLEX_H
#define MYCOMPLEX_H

#include

using namespace std;

class MyComplex
{
public:
explicit MyComplex(double r = 0, double i = 0);
MyComplex(const MyComplex &other);
~MyComplex();
MyComplex add(const MyComplex &other);//成员函数会有一个MyComplex *this (this指针)
void fn();
void show();
private:
double real;
double imag;
};

#endif // MYCOMPLEX_H


#include “mycomplex.h”

MyComplex::MyComplex(double r, double i) : real®, imag(i)
{

}

MyComplex::MyComplex(const MyComplex &other) : real(other.real), imag(other.imag)
{

}

MyComplex::~MyComplex()
{

}

void MyComplex::show()
{
cout << real << ‘+’ << imag << ‘i’ << endl;
}

void MyComplex::fn()
{

}

MyComplex MyComplex::add(const MyComplex &other)
{
MyComplex ret(this->real + other.real, this->imag + other.imag);
return ret;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值