数据结构与算法总结(2)

编写矩阵转置函数,输入参数3×3整型数组。

#include <iostream>
#include <cmath>
using namespace std;
void swap(int& a, int& b) {
	int temp = a;
	a = b;
	b = temp;
}

int main() {
	int a[3][3];
	cout << "input the matirx:" << endl;
	for (int i = 0; i < 3; i++) {
		for(int j=0;j<3;j++){
			cin >> a[i][j];
		}
	}
	cout << " the raw matirx:" << endl;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << a[i][j] << ' ';
		}
		cout << '\n';
	}
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < i; j++) {
			swap(a[i][j], a[j][i]);
		}
	}
	cout << " the transpose matirx:" << endl;
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < 3; j++) {
			cout << a[i][j] << ' ';
		}
		cout << '\n';
	}
	return 0;
}

函数指针,做函数回调。

//6_10.cpp
#include <iostream>
using namespace std;

int compute(int a, int b, int(*func)(int, int))
{
	return func(a, b);
}

int max(int a, int b)
{
	return((a > b) ? a : b);
}

int min(int a, int b)
{
	return((a < b) ? a : b);
}

int sum(int a, int b)
{
	return a + b;
}
int main() {
	int a, b, res;
	cout << "input int a:"; cin >> a;
	cout << "input int b:"; cin >> b;
	res = compute(a, b, &max);
	cout << "Max of " << a << " and " << b << " is " << res << endl;
	return 0;
}

动态数组类,注意构造函数和析构函数new delete的配合,思考Point& element(int index),为什么是Point引用类型,返回“引用”可以用来操作封装数组对象内部的数组元素。如果返回“值”则只是返回了一个“副本”,通过“副本”是无法操作原来数组中的元素的。

#include <iostream>

#include <cassert>

using namespace std;

class Point { //类的声明同例6-16 … };

class ArrayOfPoints { //动态数组类

public:

ArrayOfPoints(int size) : size(size) {

points = new Point[size];

}

~ArrayOfPoints() {

cout << "Deleting..." << endl;

delete[] points;

}

Point& element(int index) {

assert(index >= 0 && index < size);

return points[index];

}

private:

Point *points; //指向动态数组首地址

int size; //数组大小

};

int main() {

int count;

cout << "Please enter the count of points: ";

cin >> count;

ArrayOfPoints points(count); //创建数组对象

points.element(0).move(5, 0); //访问数组元素的成员

points.element(1).move(15, 20); //访问数组元素的成员

return 0;

}

下面的代码声明了两个基类Base1和Base2,然后从这两个基类按照公有方式派生出类Derived。基类和派生类都各自包含一个公有成员x,并且Base1和Base2各有接受一个整型参数的构造函数,Derived的构造函数接受Base1和Base2的对象引用a,b来初始化Derived类对象,并令x为Base1::x和Base2::x之和。

#include "pch.h"
#include <iostream>
using namespace std;

struct Base1
{
	int x;
	Base1(int x);
};
Base1::Base1(int x)
	:x(x) {}

struct Base2
{
	int x;
	Base2(int x);
};
Base2::Base2(int x)
	:x(x) {}

struct Derived :public Base1, public Base2
{
	int x;
	Derived(Base1& a, Base2& b);
};
Derived::Derived(Base1& a, Base2& b)
	:Base1(a), Base2(b) {
	x = Base1::x + Base2::x;  //排除二义性,须在x前知名Base。
}

//请实现Base1,Base2, Derived的构造函数

int main()
{
	int x, y;
	cin >> x >> y;
	Base1 a(x);
	Base2 b(y);
	Derived d(a, b);
	cout << d.Base1::x << "+" << d.Base2::x << "=" << d.x << endl;
	return 0;
}

声明一个车(vehicle)基类,既有MaxSpeed,Weights等成员变量,Run,Stop等成员函数,由此派生出自行车(bicycle)类,汽车(motorcar)类。
其中自行车类又高度等属性,汽车类有座位数等属性
从bicycle和motorcar派生出摩托车motorcycle类,在继承过程中,注意吧vehicle设置为虚基类。

// 把vehicle设置为非虚基类会报错,出现二义性问题
//

//6_25.cpp
#include "pch.h"
#include <iostream>
using namespace std;

class Vehicle
{
public:
	int MaxSpeed;
	int Weight;
	void Run() { cout << "Vehicle run" << endl; };
	void Stop() { cout << "Vehicle stop" << endl; };
};

class Bicycle :virtual public Vehicle
{
public:
	int Height;
};

class Motorcar :virtual public Vehicle
{
public:
	int SeatNum;
};

class Motorcycle :public Bicycle, public Motorcar
{

};
int main()
{
	Motorcycle m;
	m.Height = 1;
	m.Run();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值