C++面向对象复习笔记暨备忘录

C++指针

指针作为形参

交换两个实际参数的值

#include <iostream>
#include<cassert>
using namespace std;

int swap(int *x, int* y) {
	int a;
	a = *x;
	*x = *y;
	*y = a;
	return 0;
}
int main() {
	int a = 1;
	int b = 2;
	swap(&a, &b);
	cout << a << " " << b << endl;
}

数组作为函数参数

#include <iostream>
#include<cassert>
using namespace std;

void f(int arr[]) {
	cout << sizeof(arr) << endl;
}
int main() {
	int array[5] = { 1,2,3,4,5 };
	cout << sizeof(array) << endl;

	f(array);
	return 0;
}

运行结果:

C++面向对象

定义类的示例程序

编写一个程序,输入矩形的长和宽,计算矩形的面积和周长。

#include <iostream>
#include<cassert>
using namespace std;

class CRectangle {
public:
	int w, h;
	void init(int w_, int h_);
	int area();
	int perimeter();
};

void CRectangle::init(int w_, int h_) {
	w = w_; h = h_;
}

int CRectangle::area() {
	return w * h;
}
	

int CRectangle::perimeter() {
	return 2 * (w + h);
}
	
int main() {
	CRectangle r;
	r.init(3, 5);
	cout << "area is:" << r.area() << endl;
	cout << "perimeter is:" << r.perimeter() << endl;
	cout << sizeof(CRectangle) << endl;
	return 0;
}

不同方式调用构造函数示例

#include <iostream>
#include<cassert>
using namespace std;

class Complex {
private:
	double real, imag;
public:
	Complex(double r);
	void print_info();
};

Complex::Complex(double r) {
	real = r;
	imag = 0;
}

void Complex::print_info() {
	cout << real << endl;
	cout << imag << endl;
}
	
int main() {
	Complex r3(4.12);
	r3.print_info();

	Complex r4 = 7;
	r4.print_info();
}

成员函数和封闭类

#include<iostream>
using namespace std;

class CTyre {
private:
	int radius;
	int width;

public:
	CTyre(int r, int w) :radius(r), width(w) {
		cout << "CTyre contructor" << endl;
	}

	~CTyre() {
		cout << "CTyre distructor" << endl;
	}
};

class CEngine{
public:
	CEngine() {
		cout << "CEngine contructor" << endl;
	}

	~CEngine() {
		cout << "CEngine distructor" << endl;
	}
};

class CCar {
private:
	int price;
	CTyre ctyre;
	CEngine engine;

public:
	CCar(int p, int w, int r);
	~CCar();
};

CCar::CCar(int p, int w, int r) : price(p), ctyre(w, r) {
	cout << "CCar contructor" << endl;
};

CCar::~CCar() {
	cout << "CCar distructor" << endl;
}

int main() {
	CCar car(200000, 17, 35);
	return 0;
}

 

C++11扩展

nullptr

如果有重载函数void f(int *x)和void f(int x)

那么,f(NULL)将会调用f(int x),这肯定不是程序员的原意。

C++11引入新的关键字nullptr, 充当单独的空指针常量。调用f(NULL)将会调用f(int* x)

auto

如果申请动态变量时给出初值,就可以使用auto推断所需分配的对象类型。例如:

#include <iostream>
#include<cassert>
using namespace std;


int main() {
	auto p1 = new auto(10);
	cout << *p1 << endl;
	delete p1;
	return 0;
}

动态数组初始化

C++11允许为动态数组指定初值。

#include <iostream>
#include<cassert>
using namespace std;


int main() {
	int* p = new int[5] {1, 2, 3, 4, 5};
	cout << p[0] << endl;
	delete p;
	return 0;
}

生成默认构造函数

在类定义时,如果定义了构造函数,则编译器不再生成默认构造函数。如果希望仍保留编译器默认的构造行为,可以使用=default要求编译器生成一个默认的构造函数和复制构造函数。

#include <iostream>
using namespace std;

class Dog {
private:
    int age;
    int hight;
public:
    Dog() = default;
    void set_age(int a);
    int get_age() {
        return age;
    }
};

void Dog::set_age(int a) {
    age = a;
}


// 程序的主函数
int main()
{
    Dog d;
    d.set_age(5);
    cout << d.get_age() << endl;
}

阻止拷贝

C++11中,可以使用关键字"=delete"禁用某个功能。如果某个类不允许复制构造,可以在类中声明。

#include <iostream>
using namespace std;

class Dog {
private:
    int age;
    int hight;
public:
    Dog() = default;
    Dog(const Dog&) = delete;
    void set_age(int a);
    int get_age() {
        return age;
    }
};

void Dog::set_age(int a) {
    age = a;
}


// 程序的主函数
int main()
{
    Dog d;
    d.set_age(5);
    cout << d.get_age() << endl;
}

委托构造

C++11允许一个构造函数调用另一个构造函数

#include <iostream>
using namespace std;

class Dog {
private:
    int age;
    int hight;
public:
    Dog() = default;
    Dog(int a);
    Dog(int age, int hight);
    Dog(const Dog&) = delete;
    void set_age(int a);
    int get_age() {
        return age;
    }
};

Dog::Dog(int a) {
    age = a;
}

Dog::Dog(int a, int h) : Dog(a) {
    hight = h;
}

void Dog::set_age(int a) {
    age = a;
}


// 程序的主函数
int main()
{
    Dog d(5, 10);
    cout << d.get_age() << endl;
}

。调用其他构造函数的构造函数称为委托构造函数。

初始化列表

C++11允许使用花括号括起来的一组值,称为初始化列表,作为赋值表达式的右运算对象。如果左边对象是内置类型,那么初始化列表只能包含一个值。对于类类型,赋值的细节由类设计者决定。默认情况下是将初始化列表中的值依次赋给数据成员。

#include <iostream>
using namespace std;

class Dog {
private:
    //类内初始化
    int age = 0;
    int hight;
public:
    Dog() = default;
    Dog(int a);
    Dog(int age, int hight);
    Dog(const Dog&) = delete;
    void set_age(int a);
    int get_age() {
        return age;
    }
};

Dog::Dog(int a) {
    age = a;
}

Dog::Dog(int a, int h) : Dog(a) {
    hight = h;
}

void Dog::set_age(int a) {
    age = a;
}


// 程序的主函数
int main()
{
    Dog d1(5, 10);
    cout << d1.get_age() << endl;

    Dog d2 = { 3,9 };
    cout << d2.get_age() << endl;

    int i = { 10 };
    cout << i << endl;
}

类内初始化

见上方代码中的注释。

  • 12
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值