C++ 多继承
本节将讨论C++编程语言中的多继承。当我们将一个类的特性和功能继承到另一个类时,这个过程被称为 继承 。通过派生类对象,我们可以重用、扩展或修改父类的所有属性和行为。这是面向对象编程的最重要特性之一,可以减少程序的长度。
从另一个或父类继承所有成员函数和功能的类被称为 派生类 。而派生类从哪个类中获得某些特性则被称为 基类或父类 。
多重继承 是C++中继承的概念,它允许子类从多个 基类 中继承属性或行为。因此,我们可以说它是使派生类能够从多个基类中获取成员函数、属性和特征的过程。
多重继承的图示
以下是C++编程语言中多重继承的图示。
在上面的图表中,有两个父类: 基类1 和 基类2 ,而只有一个 子类 。子类继承了基类1和基类2的所有特性。因此,我们将这种继承称为多重继承。
多重继承的语法
class A
{
// code of class A
}
class B
{
// code of class B
}
class C: public A, public B (access modifier class_name)
{
// code of the derived class
}
在上述语法中,类A和类B是两个基类,类C是继承了父类的一些特性的子类。
我们来编写多重继承的多个程序,使用派生类从多个基类继承成员函数和功能。
示例1:使用多重继承的程序
Program1.cpp
#include
using namespace std;
// create a base class1
class Base_class
{
// access specifier
public:
// It is a member function
void display()
{
cout << " It is the first function of the Base class " << endl;
}
};
// create a base class2
class Base_class2
{
// access specifier
public:
// It is a member function
void display2()
{
cout << " It is the second function of the Base class " << endl;
}
};
/* create a child_class to inherit features of Base_class and Base_class2 with access specifier. */
class child_class: public Base_class, public Base_class2
{
// access specifier
public:
void display3() // It is a member function of derive class
{
cout << " It is the function of the derived class " << endl;
}
};
int main ()
{
// create an object for derived class
child_class ch;
ch.display(); // call member function of Base_class1
ch.display2(); // call member function of Base_class2
ch.display3(); // call member function of child_class
}
输出
It is the first function of the Base class
It is the second function of the Base class
It is the function of the derived class
在上面的程序中,我们创建了两个基类和一个子类。子类调用了父类Base_class和Base_class2中的成员函数display()和display2(),并通过子类的对象ch实现了调用。
示例2:使用多重继承进行算术操作
让我们创建一个派生类,从多个基类中继承成员函数来执行C++编程。
Program2.cpp
#include <iostream>
using namespace std;
// create add class
class add
{
public:
int x = 20;
int y = 30;
void sum()
{
cout << " The sum of " << x << " and " << y << " is " <<x+y << endl;
}
};
// create Mul class
class Mul
{
public:
int a = 20;
int b = 30;
void mul()
{
cout << " The Multiplication of " << a << " and " << b << " is " <<a*b << endl;
}
};
// create Subclass
class Sub
{
public:
int a = 50;
int b = 30;
void sub()
{
cout << " The Subtraction of " << a << " and " << b << " is " <<a-b << endl;
}
};
// create Div class
class Div
{
// access specifier
public:
int a = 150;
int b = 30;
void div()
{
cout << " The Division of " << a << " and " << b << " is " <<a/b << endl;
}
};
// create a derived class to derive the member functions of all base classes
class derived: public add, public Div, public Sub, public Mul
{
// access specifier
public:
int p = 12;
int q = 5;
void mod()
{
cout << "The Modulus of " << p << " and " <<q << " is " << p % q << endl;
}
};
int main ()
{
// create an object of the derived class
derived dr;
dr.mod(); // call derive class member function
// call all member function of class add, Div, Sub and Mul
dr.sum();
dr.mul();
dr.div();
dr.sub();
}
输出
The Modulus of 12 and 5 is 2
The sum of 20 and 30 is 50
The Multiplication of 20 and 30 is 600
The Division of 150 and 30 is 5
The Subtraction of 50 and 30 is 20
示例3:使用多重继承获取六门科目的平均分
让我们在C++编程语言中创建另一个程序来打印使用多重继承获取六门科目的平均分。
Program3.cpp
#include
using namespace std;
// create base class1
class student_detail
{
// access specifier
protected:
int rno, sum = 0, i, marks[5];
// access specifier
public:
void detail()
{
cout << " Enter the Roll No: " << endl;
cin >> rno;
cout << " Enter the marks of five subjects " << endl;
// use for loop
for (i = 0; i < 5; i++)
{
cin >> marks[i];
}
for ( i = 0; i < 5; i++)
{
// store the sum of five subject
sum = sum + marks[i];
}
}
};
// create base class2
class sports_mark
{
protected:
int s_mark;
public:
void get_mark()
{
cout << "\n Enter the sports mark: ";
cin >> s_mark;
}
};
/* create a result as the child class to inherit functions of the parent class: student_detail and sports_mark.*/
class result: public student_detail, public sports_mark
{
int tot, avg;
public:
// create member function of child class
void disp ()
{
tot = sum + s_mark;
avg = tot / 6; // total marks of six subject / 6
cout << " \n \n \t Roll No: " << rno << " \n \t Total: " << tot << endl;
cout << " \n \t Average Marks: " << avg;
}
};
int main ()
{
result obj; // create an object of the derived class
// call the member function of the base class
obj.detail();
obj.get_mark();
obj.disp();
}
输出
Enter the Roll No:
25
Enter the marks of five subjects
90
85
98
80
75
Enter the sports mark: 99
Roll No: 25
Total: 527
Average Marks: 87
多重继承中的歧义问题
在多重继承中,当一个类从两个或更多基类或父类派生时。因此,可能存在两个父类具有同名成员函数的情况,在子类对象调用同名成员函数时会出现歧义。因此,我们可以说,C++编译器在选择类的成员函数来执行程序时会感到困惑。
演示多重继承中的歧义问题的程序
让我们编写一个简单的C++程序,通过派生类调用父类的同一个成员函数。
Program4.cpp
#include
#include
using namespace std;
// create class A
class A
{
public:
void show()
{
cout << " It is the member function of class A " << endl;
}
};
// create class B
class B
{
public:
void show()
{
cout << " It is the member function of class B " << endl;
}
};
// create a child class to inherit the member function of class A and class B
class child: public A, public B
{
public:
void disp()
{
cout << " It is the member function of the child class " << endl;
}
};
int main ()
{
// create an object of the child class to access the member function
child ch;
ch.show(); // It causes ambiguity
ch.disp();
return 0;
}
当上面的程序编译时,它会抛出show()成员函数有歧义的错误。因为基类 A 和 B 都定义了相同的成员函数 show(),当派生类的对象调用 shows() 函数时,在多重继承中会出现歧义。
因此,我们需要解决多重继承中的歧义问题。可以通过在子类中定义 类名 和 作用域解析(::)运算符 来解决歧义问题,从而指定成员函数所在的类。
歧义解决的语法
Derived_obj_name.parent_class_name : : same_named_memberFunction ( [parameter] );
例如,
ch.A:: show(); // class_name and scope resolution operator with member function
ch.B::show();
在进行一些修改后,现在我们再次执行上述程序,返回以下给定的输出。
It is the member function of the child class
It is the member function of class A
It is the member function of class B