虚幻Unreal - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(86评价)
价格: 3069.00元

静态成员变量

static 基类和派生类 使用的同一个

必须要初始化

静态成员函数,只能在类的内部定义,只能使用静态成员变量

[展开全文]
simple7503 · 2021-06-04 · 0

1构造顺序

基类

子对象

派生类

2析构顺序

派生类

子对象

基类

[展开全文]
simple7503 · 2021-06-04 · 0

派生类的析构调用顺序 相反

先调用派生类的析构 再调用基类的

[展开全文]
simple7503 · 2021-06-03 · 0

在派生类的外部访问基类中的成员时,会根据继承方式影响基类成员的访问级别

[展开全文]
simple7503 · 2021-06-03 · 0

子类调用父类的方法

子类.父类::fun()

[展开全文]
simple7503 · 2021-06-03 · 0

带引用类型返回值的函数  不要返回临时变量的引用

[展开全文]
simple7503 · 2021-06-03 · 0

友元函数

1.写在类的外部,声明在类的里面,可以通过类的对象去访问类的私有成员

2.使用friend 关键字

class Student{

 friend void pass(Student &s);

}

void pass(Student &s){

}

[展开全文]
simple7503 · 2021-06-03 · 0

访问控制

级别:public < protected < private

类的继承方式和内部的成员,取级别高的

[展开全文]
黄俊豪 · 2020-06-23 · 0

概念:

1.重载:在同一作用域下,函数名相同,但参数列表不同。

2.重写:分别在基类和派生类中(在不同的作用域下),函数名相同,参数相同,返回类型相同,并且基类函数必须为虚函数。

3.重定义:分别在基类和派生类中(在不同作用域下),函数名相同,不不构成重写的话则为重定义。(参数列表可相同可不同)。

[展开全文]
史小墙 · 2020-06-20 · 0

完成程序:职业系统

要求:

1.设计一个包含所有职业共同属性和方法的基类(HP,MP,DEF,RES,普通攻击)

2.设计合集三个不同的派生类(Warrior,Mage,Priest),各个派生类特有的属性(战士:ATN,法师,牧师:INT)不同职业拥有各自不同的技能

(战士:刀斩,法师:火球术,牧师:治疗术)

3.创建各个对象并使用技能。

 

 

 

[展开全文]
史小墙 · 2020-06-18 · 0
#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
class B
{
public:
	int b_;
	B(int b = 0)
	{
		b_ = b;
		cout << "调用B的构造函数" << endl;
	}
};
class C1 :virtual public B
{
public:
	int c1_;
	C1(int b = 0, int c1 = 0) :B(b)
	{
		c1_ = c1;
		cout << "调用C1的构造函数" << endl;
	}
};
class C2 :virtual public B
{
public:
	int c2_;
	C2(int b = 0, int c2 = 0) :B(b)
	{
		c2_ = c2;
		cout << "调用C2的构造函数" << endl;
	}
};
class D :public C1, public C2
{
public:
	int d_;
	D(int b = 0, int c1 = 0, int c2 = 0, int d = 0) :B(b), C1(b, c1), C2(b, c2)
	{
		d_ = d;
		cout << "调D的构造函数" << endl;
	}
	void printf()
	{
		cout << "  C1.b_: " << C1::b_ << "  C2.b_: " << C2::b_ << "  c1_: " << c1_ << "  c2_: " << c2_ << "  d_: " << d_ << endl;
	}
};
class D2 : public C1, public C2
{
public:
	int d2_;
	D2(int b = 0, int c1 = 0, int c2 = 0, int d2 = 0) :B(b), C1(b, c1), C2(b, c2)
	{
		d2_ = d2;
		cout << "调用D2的构造函数" << endl;
	}
	void printf()
	{
		cout << "b_: " << b_ << "  c1_: " << c1_ << "  c2_: " << c2_ << "  d_: " << d2_ << endl;
	}
};
int main()
{
	D d = D(1, 2, 3, 4);
	d.printf();
	D2 d2 = D2(3, 4, 5, 6);
	d2.printf();


}

 

[展开全文]
史小墙 · 2020-06-18 · 0
#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
class Student 
{
protected:
	string name_;
	int age_;
public:
	void setStudent(string name, int age)
	{
		name_ = name;
		age_ = age;
	}
	void printf()
	{
		cout << "姓名: " << name_ << "  年龄: " << age_ << endl;
	}
	int get_age()
	{
		return age_;
	}
	string get_name()
	{
		return name_;
	}
};
class Collagestudent:public Student 
{
private:
	string major_;
public:
	void setCStudent(string name, int age, string major)
	{
		name_ = name;
		age_ = age;
		major_ = major;
	}
	void printf()
	{
		cout << "姓名: " << name_ << "  年龄: " << age_ << "  专业: "<<major_<<endl;
	}
};
void work(Student &s)
{
	if (s.get_age() >=18)
		cout <<s.get_name()<< "达到法定工作年龄" << endl;
	else
		cout << s.get_name() << "未达到法定工作年龄" << endl;
}

int main()
{
	Student stu;
	stu.setStudent("李明",15);
	stu.printf();
	Collagestudent cstu;
	cstu.setCStudent("李雷", 18, "计算机");
	cstu.printf();
	Student stu1 = cstu;//将派生类对象赋值给基类对象
	stu1.printf();
	Student& rs = cstu;//将派生类的对象初始化基类的引用
	rs.printf();
	Student* ps = &cstu;//可以将派生类对象的地址赋给基类指针
	ps->printf();
	work(stu);//使用基类对象作为形参
	work(cstu);//使用派生类对象作为形参

}

 

[展开全文]
史小墙 · 2020-06-17 · 0
#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
class Base
{
private:
    int bnum1_;
public://在类的内部都可以访问到,通过成员方法或者友元函数。
    //在派生类中只能访问到基类的public成员和protected成员。
    int bnum2_;
    void setNum(int bnum1, int bnum2, int bnum3)
    {
        bnum1_ = bnum1;
        bnum2_ = bnum2;
        bnum3_ = bnum3;
    }
    friend  void printf(Base &b)
    {
        cout << "  bnum1: " << b.bnum1_ << "  bnum2: " << b.bnum2_ << "  bnum3: " << b.bnum3_ << endl;
    }
protected:
    int bnum3_;
};
class Derived1 :private Base
{
public:
    void printf()
    {
        //基类私有成员不能被访问。
        cout << "bnum2: " << bnum2_ << "  bnum3: " << bnum3_ << endl;
    }
};
class Derived2 :protected Base
{
    void printf()
    {
        //基类私有成员不能被访问。
        cout << "bnum2: " << bnum2_ << "  bnum3: " << bnum3_ << endl;//基类的公有成员和保护成员,在派生类中可以访问。
    }
};
class Derived : public Base
{
private:
    int bnum1_;
public:
    int bnum2_;
protected:
    int bnum3_;
};

int main()
{
    Base b;
    b.bnum2_=10;
    //b.bnum1_=10;//不可以访问
    //b.bnum3_=10;//不可以访问
    b.setNum(20, 25, 30);
    printf(b);
    Derived d;
    d.bnum2_ = 10;//可以被访问
    
}

 

[展开全文]
史小墙 · 2020-06-17 · 0

实现一个Money类

要求:

1.Money类有两个数据成员:金币数量和银币数量,每一个金币等于1000个银币。

2.实现+,-,*,/。

3.实现++,--运算符的重载(银币加减100)。

4.实现=运算符的重载。

5.实现==,!=,>=,<=,<,>运算符的重载。

6.实现>>,<<运算符的重载。

#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
class Money
{
private:
	float gold_;
	float silver_;
	static const int ratio = 1000;
public:
	Money(int gold = 0, int silver = 0);
	~Money();
	Money operator+(const Money& m);
	Money operator-(const Money& m);
	Money operator*(const int n);
	Money operator/(const int n);
	Money& operator++();
	Money& operator--();
	Money& operator=(const Money& m);
	bool operator==(const Money& m);
	bool operator!=(const Money& m);
	bool operator>=(const Money& m);
	bool operator<=(const Money& m);
	bool operator<(const Money& m);
	bool operator>(const Money& m);
	friend ostream& operator<<(ostream& os, const Money& m);
	friend istream& operator>>(istream& is, Money& m);



};

int main()
{
	Money m1 = Money(100, 600);
	Money m2 = Money(100, 500);
	Money m3 = m1;
	Money m4 = m3 + m2;
	Money m5 = m1 * 2;
	++m3;
	Money m6 = m5 / 2;
	cout << m1 << endl << m2 << endl << m3 << endl << m4 << endl << m5 << endl << m6 << endl;
	Money m7;
	cout << "请输入m7的值: ";
	cin >> m7;
	Money m8;
	cout << "请输入m8的值: ";
	cin >> m8;
	if (m7 == m8)
		cout << "m7等于m8" << endl;
	else if (m7 > m8)
	{
		cout << "m7大于m8" << endl;
	}



}

Money::Money(int gold, int silver)
{
	gold_ = gold;
	silver_ = silver;
}

Money::~Money() {}

Money Money::operator+(const Money& m)
{
	Money money;
	int i = this->gold_ * ratio + m.gold_ * ratio + this->silver_ + m.silver_;
	money.gold_ = i / ratio;
	money.silver_ = i % ratio;
	return money;
}

Money Money::operator-(const Money& m)
{
	Money money;
	int i = this->gold_ * ratio - m.gold_ * ratio + this->silver_ - m.silver_;
	money.gold_ = i / ratio;
	money.silver_ = i % ratio;
	return money;
}

Money Money::operator*(const int n)
{
	Money money;
	int i = this->gold_ * ratio * n + this->silver_ * n;
	money.gold_ = i / ratio;
	money.silver_ = i % ratio;
	return money;
}

Money Money::operator/(const int n)
{
	Money money;
	int i = (this->gold_ * ratio + this->silver_) / n;
	money.gold_ = i / ratio;
	money.silver_ = i % ratio;
	return money;
}

Money& Money::operator++()
{
	int i = this->gold_ * ratio + this->silver_;
	i += 100;
	this->gold_ = i / ratio;
	this->silver_ = i % ratio;
	return*this;
}

Money& Money::operator--()
{
	int i = this->gold_ * ratio + this->silver_;
	i -= 100;
	this->gold_ = i / ratio;
	this->silver_ = i % ratio;
	return*this;
}

Money& Money::operator=(const Money& m)
{
	this->gold_ = m.gold_;
	this->silver_ = m.silver_;
	return *this;
}

bool Money::operator==(const Money& m)
{
	if ((this->gold_ * ratio + this->silver_) == (m.gold_ * ratio + m.silver_))
		return true;
	else
		return false;
}

bool Money::operator!=(const Money& m)
{
	if ((this->gold_ * ratio + this->silver_) != (m.gold_ * ratio + m.silver_))
		return true;
	else
		return false;
}

bool Money::operator>=(const Money& m)
{
	if ((this->gold_ * ratio + this->silver_) >= (m.gold_ * ratio + m.silver_))
		return true;
	else
		return false;
}

bool Money::operator<=(const Money& m)
{
	if ((this->gold_ * ratio + this->silver_) <= (m.gold_ * ratio + m.silver_))
		return true;
	else
		return false;
}

bool Money::operator<(const Money& m)
{
	if ((this->gold_ * ratio + this->silver_) < (m.gold_ * ratio + m.silver_))
		return true;
	else
		return false;
}

bool Money::operator>(const Money& m)
{
	if ((this->gold_ * ratio + this->silver_) > (m.gold_ * ratio + m.silver_))
		return true;
	else
		return false;
}

ostream& operator<<(ostream& os, const Money& m)
{
	os << "金币数量:" << m.gold_ << "  " << "银币数量:" << m.silver_ << endl;
	return os;

}

istream& operator>>(istream& is, Money& m)
{
	is >> m.gold_ >> m.silver_;
	if (!is)
	{
		m = Money();
	}
	return is;


}

 

[展开全文]
史小墙 · 2020-06-16 · 0
#include <iostream>
#include "stdio.h"
#include "string"
using namespace std;
class Time
{
private:
	int hours_;
	int minutes_;
public:
	Time(int hours = 0, int minutes = 0);
	~Time();
	void showTime();
	Time operator+(const Time& t);
	Time operator-(const Time& t);
	Time operator*(int const i);
};

int main()
{
	Time t1 = Time(19, 55);
	Time t2 = Time(1, 5);
	Time t3 = Time(5, 30);
	//t1.showTime();
	Time total1 = t1 + t2;
	Time total2 = t1.operator-(t3);
	Time total3 = t3.operator*(2);
	Time total4 = t1 + t3 * 2;
	total1.showTime();
	total2.showTime();
	total3.showTime();
	total4.showTime();
}

Time::Time(int hours, int minutes)
{
	hours_ = hours;
	minutes_ = minutes;
}

Time::~Time() {}

void Time::showTime()
{
	cout << "当前时间为:" << hours_ << "小时" << minutes_ << "分" << endl;
}

Time Time::operator+(const Time& t)
{
	Time Temp;
	Temp.hours_ = this->hours_ + t.hours_;
	if (this->minutes_ + t.minutes_ < 60)
		Temp.minutes_ = this->minutes_ + t.minutes_;
	else
	{
		Temp.hours_ += 1;
		Temp.minutes_ += (this->minutes_ + t.minutes_) % 60;
	}

	return Temp;

}

Time Time::operator-(const Time& t)
{
	Time Temp;
	float i = this->hours_ * 60 + this->minutes_ - t.hours_ * 60 - t.minutes_;
	Temp.hours_ = i / 60;
	Temp.minutes_ = i - Temp.hours_ * 60;
	return Temp;
}

Time Time::operator*(int const i)
{
	int j;
	Time temp;
	j = this->hours_ * i * 60 + this->minutes_ * i;
	temp.hours_ = j / 60;
	temp.minutes_ = j - temp.hours_ * 60;
	return temp;

}

 

[展开全文]
史小墙 · 2020-06-14 · 0

1)const成员函数可以访问非const对象的非const数据成员、const数据成员,也可以访问const对象内的所有数据成员;

2)非const成员函数可以访问非const对象的非const数据成员、const数据成员,但不可以访问const对象的任意数据成员

3)作为一种良好的编程风格,在声明一个成员函数时,若该成员函数并不对数据成员进行修改操作,应尽可能将该成员函数声明为const 成员函数。

4)如果只有const成员函数,非const对象是可以调用const成员函数的。当const版本和非const版本的成员函数同时出现时,非const对象调用非const成员函数。

[展开全文]
极光流刃 · 2019-09-20 · 1

C++中一般情况下的联编是静态联编。一旦涉及到多态和虚函数就必须使用动态联编。

[展开全文]
极光流刃 · 2019-09-19 · 1

1.一般为了便于阅读,派生类的虚函数也会添加上virtual关键字;

2.使用基类的对象的引用作为参数,可以根据传递的实参类型,去选择调用的函数。

[展开全文]
极光流刃 · 2019-09-19 · 1

静态成员函数只能访问静态数据成员和静态成员函数。

[展开全文]
极光流刃 · 2019-09-18 · 1

一般来说,系统会自动提供一个默认的拷贝构造函数和赋值符重载函数;

但当类中包含有指针或任何运行时分配的资源时,则需要我们自定义赋值操作符重载函数与拷贝构造函数。

[展开全文]
极光流刃 · 2019-09-16 · 1