c++学习

1.某学校教职工的工资计算方法为:
2.1.所有教职工都有基本工资
3.2.教师月工资为固定工资+课时补贴,课时补贴根据职称号不同有所不同,例如
4.每课时教授补贴50元,副教授补贴30元,讲师补贴20元。
5.3.管理人员月薪为基本工资+职务工资。
6.4.实验室人员月薪为基本工资+工作日补贴,工日日补贴等于日补贴*月工作日数
7.定义教职工抽象类,派生教师类,管理人员类和实验室类,编写程序测试这个类体系

#include <iostream>
using namespace std;

class Employee {   // 定义教职工抽象类
protected:
	string name;   // 姓名
	int basic_salary;   // 基本工资
public:
	Employee(string n, int s) : name(n), basic_salary(s) { }
	virtual int get_salary() = 0;   // 纯虚函数,子类必须实现
	virtual void printInfo() = 0;   // 纯虚函数,子类必须实现
};

class Teacher : public Employee {   // 定义教师类
private:
	int teach_hours;   // 教学课时
	string title;   // 职称
public:
	Teacher(string n, int s, int h, string t) : Employee(n, s), teach_hours(h), title(t) { }
	int get_salary() {
		double teachingAllowance = 0;
		if (title == "教授") {
			teachingAllowance = teach_hours * 50;
		}
		else if (title == "副教授") {
			teachingAllowance = teach_hours * 30;
		}
		else if (title == "讲师") {
			teachingAllowance = teach_hours * 20;
		}
		return basic_salary + teachingAllowance;
	}
	void printInfo() {
		cout << "教师姓名:" << name << endl;
		cout << "基本工资:" << basic_salary << endl;
		cout << "职称:" << title << endl;
		cout << "课时数:" << teach_hours << endl;
		cout << "月工资:" << get_salary() << endl;
	}
};
class Manager : public Employee {   // 定义管理人员类
private:
	int duty_salary;    // 职务工资
public:
	Manager(string n, int s, int d) : Employee(n, s), duty_salary(d) { }
	int get_salary() {
		return basic_salary + duty_salary;
	}
	void printInfo() {
		cout << "管理人员姓名:" << name << endl;
		cout << "基本工资:" << basic_salary << endl;
		cout << "职务工资:" << duty_salary << endl;
		cout << "月工资:" << get_salary() << endl;
	}
};

class LabStaff : public Employee {   // 定义实验室人员类
private:
	int workdays_per_month;   // 每月工作日数
	int daily_allowance;   // 日补贴
public:
	LabStaff(string n, int s, int d, int a) : Employee(n, s), workdays_per_month(d), daily_allowance(a) { }
	int get_salary() {
		return basic_salary + workdays_per_month * daily_allowance;
	}
	void printInfo() {
		cout << "实验室人员姓名:" << name << endl;
		cout << "基本工资:" << basic_salary << endl;
		cout << "日补贴:" << daily_allowance << endl;
		cout << "月工作日数:" << workdays_per_month << endl;
		cout << "月工资:" << get_salary() << endl;
	}
};

/*这个程序定义了一个Employee类作为教职工的抽象类,派生出Teacher类、Manager类和LabStaff类分别表示教师、管理人员和实验室人员。这些子类都实现了get_salary()纯虚函数,计算每个员工的月薪。

在main()函数中,我们创建了一个指向Employee对象的指针数组,数组中存放了4个不同类型的员工。然后我们遍历数组,*/
int main() {
	Employee* employees[4]; // 定义一个指向Employee对象的指针数组

	employees[0] = new Teacher("张三", 5000, 80, "教授");
	employees[1] = new Manager("李四", 6000, 2000);
	employees[2] = new LabStaff("王五", 4000, 22, 30);
	employees[3] = new Teacher("赵六", 4500, 60, "副教授");

	for (int i = 0; i < 4; i++) {
		employees[i]->printInfo();
	}

	return 0;
}


8.1.编写程序定义类MyArray
9.2.重载运算符“+”、“-”、“*”和“=”,实现数组直接的加、减、乘、赋值运算
10.3.重载运算符“>>”、“<<”,实现向量输入输出功能
11.4.测试运算的组合算法
#include <iostream>
using namespace std;

class MyArray {
private:
	int size;
	int* data;
public:
	MyArray(int s = 0) :size(s) {   // 构造函数
		data = new int[size];
	}

	~MyArray() {   // 析构函数
		delete[] data;
	}

	MyArray(const MyArray& other) {   // 拷贝构造函数
		size = other.size;
		data = new int[size];
		for (int i = 0; i < size; i++) {
			data[i] = other.data[i];
		}
	}

	MyArray operator+(const MyArray& other) const {   // 重载运算符+
		if (size != other.size) {
			cout << "两个数组大小不一致,不能相加" << endl;
			exit(-1);
		}
		MyArray result(size);
		for (int i = 0; i < size; i++) {
			result.data[i] = data[i] + other.data[i];
		}
		return result;
	}

	MyArray operator-(const MyArray& other) const {   // 重载运算符-
		if (size != other.size) {
			cout << "两个数组大小不一致,不能相减" << endl;
			exit(-1);
		}
		MyArray result(size);
		for (int i = 0; i < size; i++) {
			result.data[i] = data[i] - other.data[i];
		}
		return result;
	}

	MyArray operator*(const MyArray& other) const {   // 重载运算符*
		if (size != other.size) {
			cout << "两个数组大小不一致,不能相乘" << endl;
			exit(-1);
		}
		MyArray result(size);
		for (int i = 0; i < size; i++) {
			result.data[i] = data[i] * other.data[i];
		}
		return result;
	}

	MyArray& operator=(const MyArray& other) {   // 重载运算符=
		if (this == &other) {
			return *this;
		}
		delete[] data;
		size = other.size;
		data = new int[size];
		for (int i = 0; i < size; i++) {
			data[i] = other.data[i];
		}
		return *this;
	}

	friend istream& operator>>(istream& is, MyArray& arr) {   // 重载运算符>>
		for (int i = 0; i < arr.size; i++) {
			is >> arr.data[i];
		}
		return is;
	}

	friend ostream& operator<<(ostream& os, const MyArray& arr) {   // 重载运算符<<
		for (int i = 0; i < arr.size; i++) {
			os << arr.data[i] << " ";
		}
		os << endl;
		return os;
	}
};

int main() {
	MyArray arr1(5), arr2(5), arr3(5);   // 测试向量输入输出
	cout << "请输入第一个向量:";
	cin >> arr1;
	cout << "请输入第二个向量:";
	cin >> arr2;

	arr3 = arr1 + arr2;
	cout << "arr1 + arr2 = " << arr3;

	arr3 = arr1 - arr2;
	cout << "arr1 - arr2 = " << arr3;

	arr3 = arr1 * arr2;
	cout << "arr1 * arr2 = " << arr3;

	return 0;
}

#include //头文件包含,标准输入输出流 C++有行注释、块注释,没有文件注释
using namespace std; //使用命名空间

//有且只有main函数,它是系统的唯一入口,形参可省略
int main(int argc,char *argv[])
{
cout<<”hello world”<<endl;
return 0; //主函数必须有return ;return有两个功能,一是返回一个数据类型,二是结束当前函数
}
extern int date; int date = 0; //先声明,后定义,extern只能在函数外部使用,用于声明类、函数和全局变量,一般跨文件才使用声明,同文件可以直接先定义再调用

int date = 0;int num = 0; cin>>date>>num; //键盘输入给date、num赋值,以空格或回车隔开(cin会忽略空格回车)

字符常量:’a’、’123’等 字符变量:char ch = ‘\0’;
字符串常量:”a”、”123”等 字符串变量:string str = “name”;

const关键字:const修饰的变量为只读变量,在定义变量时要初始化,被初始化后不能被赋值。

typedef关键字:给已类型或其他取个别名,如typedef int INT32,INT32就代表int关键字,typedef后面紧跟被替代的

随机函数:include <time.h> srand(time(NULL)); int a = rand();

int arr[] = {10,20,30,40,50}; int arr [5] = {10,20}; int arr = {0,[2]=10};//数组初始化、部分初始化(自动补0)

函数声明和实现:int getName(int id); //声明,就是函数除去函数体部分
string getName( int id){return name;} //实现

int &b = a; int arr[5] = {0};int (&myarr)[5] = arr; int p = num; int &myp =p;

void fun(void){} void (&myfun)(void) = fun; //别名必须初始化,对b操作与对a操作完全等价

引用的使用:定义函数void fun(int &x,int&y){int tmp = x;x = y;y = tmp;} 调用int a = 10,b = 20; fun(a,b); //相当于指针功能,但比指针方便简洁

常应用:void fun(const int &t){} //作为函数的参数,防止内部修改外部的值

内联函数:在定义的时候用inline修饰的函数,但这个函数在声明的时候不能使用inline 如inline int fun(const int &x){}

函数重载:与java规则一致

默认参数:void testfun(int x= 0,int b = 10){} //当函数调用时没有指定值,系统默认给没赋值的形参赋值,默认参数从左到右,第一个默认参数后面都必须是默认参数,如果函数存在声明,在声明处设置默认参数

占位参数:void testfun(int x= 0,int b = 10,int = 10){} //占位参数无法使用,只是告诉函数必须要给占位参数传个值,与默认参数一样,如果函数存在声明,在声明处设置占位参数

类:class A{ //类的成员变量一般设计为私有private,成员函数一般设计为公有public
private: //默认private,不能在对象外部访问,如cout<<student.a<<endl;只能被类内公共函数调用
int a;
public: //可以在对象外部访问,如cout<<student.b<<endl;
int b;
void fun(void){ … }
}; A student; //创建一个A类对象
成员函数:一般在类中声明,在类外实现
class Date{
private:
int a;
public:
void seta(int _a);
int geta();
Date() //构造函数实现和重载规则与java一致
析构函数: ~Date() //默认析构函数,清理对象使用空间的功能,完成释放内存的前置工作,析构函数没有重载,
但可以自己实现析构函数,在析构函数中添加操作,析构和创建函数遵循同级别时(同一个语句块内)先创建的后析构的规则。 一般在类中存在指针成员时,要自己定义析构函数delete释放指针空间 同时也要定义拷贝构造函数,实现深拷贝
} //类中声明成员函数

void Date::seta(int _a){a = _a;}   int Date:: geta(){return a;}	//类外实现函数

拷贝构造:用就对象给新对象赋值时系统自动调用拷贝构造函数。如果用户定义了拷贝构造、有参构造都会屏蔽无参构造,如果用户定义了有参构造和无参构造,不会屏蔽拷贝构造函数。拷贝构造函数是特殊的构造函数

Date(const Date &ob){a = ob.a(成员变量一一赋值)}		//浅拷贝Date a = b;把成员b赋给成员a

动态创建和释放对象:Date* p = new Date; Date* q = new Date(1,2); delete p;delete q; //new创建指针指向对象

this关键字:保存调用该成员函数的对象的地址 this->a = b

友元函数:一种特权函数,C++允许这个特权函数访问本类私有成员 用friend修饰,只能在声明处出现
全局函数作为友元函数:friend void getvalue(Date &data );
某类的成员函数作为友元:friend Get:: void getvalue(Date &data );
某类作为友元类:friend classGet; //任何友元都不能被继承

运算符重载:对已有运算符重新进行定义,赋予其另一种功能。
函数重载格式:返回类型 operater运算符号(参数列表){实现语句;reture 类型;}
友元运算符重载:在类中 friend返回类型 operater运算符号(参数列表)
在complex类中声明友元函数friend complex operater+(const complex &a,const complex &b);
complex operater+(const complex &a,const complex &b)
{ complex temp;
temp.real = a.real+b.real;temp.image = a.image+b.image;return temp; }

成员运算符重载:在类中直接重载运算符函数
complex operater+( const complex &b)
{ complex temp;
temp.real = this->.real+b.real; temp.image = this->image+b.image; return temp; }

<<运算符重载:只能使用友元函数重载
格式:ostream & operater<<( ostream &别名,const 类对象)
istream & operater>>( istream &别名,类对象)
实现:先在类中声明友元 ,然后
ostream & operater<<( ostream &别名,const 类对象[不要用引用]){
cout[或别名]<<b.real<<”+”<<b.image<<endl;
return 别名;}

++运算符重载:complex operater++() //前置实现++a
{ this->real++; this->image++; return *this; }

		  complex operater++(int)		//后置实现a++

{ complex temp = *this;
this->real++; this->image++; return temp; }
=运算符重载:只能使用成员函数重载
complex operater=(const complex &c)
{ this->real = c.real; this->image = c.image; return *this;}
==运算符重载:返回值是bool类型true或false

继承和派生:class A public [private or protected] B{} //父类中的private成员不能被继承 继承方式不同,子类得到的类成员封装属性不同 一般使用public继承

初始化列表:若派生类要调用基类带参数构造函数或基类只有带参数的构造函数,则需要初始化列表
格式:student(string n, int a, int g):Person(n,a)/基类构造函数/ {grade = g;/派生类只定义了grade/} //派生类初始化构造函数调用基类构造函数
多继承:格式class 继承方式 基类1,继承方式 基类2{ … }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术求索者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值