子类调用父类中的友元函数

由于友元函数并非类成员,因此不能被继承在某种需求下,可能希望子类的友元函数能够使用父类中的友元函数为此可以通过强制类型转换,将子类的指针或是引用强转为父类的引用或是指针,然后使用转换后的引用或是指针来调用基类中的友元函数。

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

class Student
{
public:
	Student(int a, int b) :_a(a), _b(b){}
	friend ostream &operator<<(ostream & out, Student & stu);

private:
	int _a;
	int _b;
};


ostream &operator<<(ostream & out, Student & stu)
{
	out << stu._a << "--" << stu._b << endl;
	return out;
}


class Graduate :public Student
{
public:
	Graduate(int a, int b, int c) :Student(a, b), _c(c){}
	friend ostream &operator<<(ostream & out, Graduate & gra);

private:
	int _c;
};


ostream &operator<<(ostream & out, Graduate & gra)
{
	cout << static_cast<Student&>(gra); 
	//友元函数不能被继承,需要转化为父类类型,然后调用其中的友元函数
	//如果不强制转化的话 参数匹配的函数还是它自己产生递归 系统崩溃
	cout <<"--"<< gra._c; 
	return out;
}



int _tmain(int argc, _TCHAR* argv[])
{
	Student s(1,2);
	cout << s ;

	Graduate g(3, 4, 5); 
	//没有自己的<<重载函数时 赋值兼容 g赋值给了Student & st 
	//赋给了父类的类型 所以才能访问父类的私有成员
	cout << g ;

	return 0;
}

 

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
非常好,下面我来给出具体的代码实现和解释每个知识点的含义。 ```C++ #include <iostream> #include <string> using namespace std; class Animal { protected: string name; int age; double weight; double height; public: Animal(string n, int a, double w, double h) : name(n), age(a), weight(w), height(h) {} virtual void displayInfo() { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; } friend Animal operator+(const Animal& a1, const Animal& a2) { Animal a("New Animal", 0, a1.weight + a2.weight, a1.height + a2.height); return a; } }; class Cat : public Animal { protected: string color; public: Cat(string n, int a, double w, double h, string c) : Animal(n, a, w, h), color(c) {} void displayInfo() override { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; cout << "Color: " << color << endl; } }; class Dog : public Animal { protected: string breed; public: Dog(string n, int a, double w, double h, string b) : Animal(n, a, w, h), breed(b) {} void displayInfo() override { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; cout << "Breed: " << breed << endl; } }; class Kitten : public Cat { protected: char gender; double weightChange; public: Kitten(string n, int a, double w, double h, string c, char g, double wc) : Cat(n, a, w, h, c), gender(g), weightChange(wc) {} Kitten(const Kitten& k) : Cat(k.name, k.age, k.weight, k.height, k.color), gender(k.gender), weightChange(k.weightChange) {} Kitten& operator=(const Kitten& k) { if (this != &k) { this->name = k.name; this->age = k.age; this->weight = k.weight; this->height = k.height; this->color = k.color; this->gender = k.gender; this->weightChange = k.weightChange; } return *this; } void displayInfo() override { cout << "Name: " << name << endl; cout << "Age: " << age << endl; cout << "Weight: " << weight << endl; cout << "Height: " << height << endl; cout << "Color: " << color << endl; cout << "Gender: " << gender << endl; cout << "Weight Change: " << weightChange << endl; } friend Kitten operator+(const Kitten& k1, const Kitten& k2) { Kitten k("New Kitten", 0, k1.weight + k2.weight, k1.height + k2.height, k1.color, 'M', 0); return k; } }; int main() { Animal* a = new Animal("Animal", 10, 20, 30); Cat* c = new Cat("Cat", 5, 10, 20, "Black"); Dog* d = new Dog("Dog", 3, 15, 25, "Husky"); Kitten* k1 = new Kitten("Kitten1", 1, 2, 3, "White", 'F', -0.5); Kitten* k2 = new Kitten("Kitten2", 2, 3, 4, "Gray", 'M', 0.5); a->displayInfo(); cout << endl; c->displayInfo(); cout << endl; d->displayInfo(); cout << endl; k1->displayInfo(); cout << endl; k2->displayInfo(); cout << endl; Animal* a_new = new Animal("New Animal", 0, 0, 0); *a_new = *a + *d; a_new->displayInfo(); cout << endl; Kitten* k_new = new Kitten("New Kitten", 0, 0, 0, "", 'M', 0); *k_new = *k1 + *k2; k_new->displayInfo(); cout << endl; delete a; delete c; delete d; delete k1; delete k2; delete a_new; delete k_new; return 0; } ``` 这个程序实现了一个动物类族,包括基类Animal和两个派生类Cat和Dog以及一个继承自Cat的子类Kitten。其,每个类都有自己的成员变量和成员函数,并且在子类可以重载父类的函数以实现不同的功能。同时,还使用了一些重载运算符、静态成员、深拷贝、虚函数等知识点来实现更加灵活的操作和更加高效的代码。 具体来说,以下是每个知识点的解释: - 静态成员:使用static关键字声明的成员,属于整个类,而不是属于某个对象,可以被类的所有对象共享。在本程序,我们可以使用静态成员来实现所有动物的个数统计等功能。 - 友元函数:使用friend关键字声明的函数,可以访问类的私有成员。在本程序,我们使用友元函数来实现运算符重载等功能。 - 运算符重载:使用operator关键字重载运算符,可以方便地对类对象进行操作。在本程序,我们重载+号来实现两个Animal对象的体重相加,以及重载+号来实现两个Kitten对象的体重相加。 - 类族对象的赋值规则:派生类的对象可以赋值给基类的对象,但是基类的对象不能赋值给派生类的对象。在本程序,我们可以将Cat对象赋值给Animal对象,但是不能将Animal对象赋值给Cat对象。 - 虚函数或纯虚函数:使用virtual关键字声明的函数,可以实现动态多态性,即同一个函数在不同的对象上有不同的实现方式。在本程序,我们使用虚函数来实现动物类的displayInfo()函数,使得每个子类都可以根据自己的需求来覆盖该函数。 - 深拷贝子对象:在派生类的构造函数和赋值运算符,需要调用父类的构造函数和赋值运算符来复制父类的数据成员,以实现深拷贝。在本程序,我们在Kitten类手动实现了深拷贝子对象的过程。 - 全局对象:使用static关键字声明的全局对象,属于整个程序,而不是属于某个函数或类。在本程序,我们可以使用全局对象来实现一些需要全局访问的功能,比如记录程序运行时间等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值