c ++ 继承_C ++继承| 查找输出程序| 套装2

news/2024/7/3 13:15:24 标签: c++, , 多态, class, object
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views">

c ++ 继承

Program 1:

class="raw_b_node">程序1:

class="has">#include <iostream>
#include <string.h>
using namespace std;

class Person {
    char name[15];
    int age;

protected:
    void SetPerson(int age, char* name)
    {
        this->age = age;
        strcpy(this->name, name);
    }
    void PrintPerson()
    {
        cout << "Name: " << name << endl;
        cout << "Age:  " << age << endl;
    }
};

class Student {
    int student_id;
    int fees;

public:
    Student(int id, int fee, int age, char* name)
    {
        student_id = id;
        fees = fee;

        SetPerson(age, name);
    }
    void Print()
    {
        cout << "Student id: " << student_id << endl;
        cout << "Fees: " << fees << endl;
        PrintPerson();
    }
};

int main()
{
    Student S(101, 5000, 5, "Shaurya");
    S.Print();
    return 0;
}

Output:

class="raw_b_node">输出:

class="has">main.cpp: In constructor ‘Student::Student(int, int, int, char*)’:
main.cpp:32:28: error: ‘SetPerson’ was not declared in this scope
         SetPerson(age, name);
                            ^
main.cpp: In member function ‘void Student::Print()’:
main.cpp:38:21: error: ‘PrintPerson’ was not declared in this scope
         PrintPerson();
                     ^

Explanation:

class="raw_b_node">说明:

It will generate errors because, we did not inherit Person class in Student class and accessing the protected members of Person class in the Student class.

因为,我们没有继承Personclass="tags" href="/tags/Lei.html" title=类>类的学生class="tags" href="/tags/Lei.html" title=类>类和访问Personclass="tags" href="/tags/Lei.html" title=类>类的protected成员在学生班会产生错误。

Program 2:

class="raw_b_node">程式2:

class="has">#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
public:
    Class1()
    {
        cout << "Class1 ctor called " << endl;
    }
};

class Class2 : public Class1 {
public:
    Class2()
    {
        cout << "Class2 ctor called " << endl;
    }
};

int main()
{
    Class2 OB;
    return 0;
}

Output:

class="raw_b_node">输出:

class="has">Class1 ctor called
Class2 ctor called

Explanation:

class="raw_b_node">说明:

Here, we created two classes Class1 and Class2. Both classes contain constructors.

在这里,我们创建了两个class="tags" href="/tags/Lei.html" title=类>类Class1Class2 。 这两个class="tags" href="/tags/Lei.html" title=类>类都包含构造函数 。

Class2 inherits Class1, in the main() function, we created the object OB of Class2. Then it called the constructor of Class1 and then it called the constructor of Class2.

Class2继承了Class1 ,在main()函数中,我们创建了Class2的对象OB 。 然后,它调用了Class1的构造函数,然后又调用了Class2的构造函数。

In C++, the base class constructor always called before the derived class constructor.

在C ++中,基class="tags" href="/tags/Lei.html" title=类>类构造函数总是在派生class="tags" href="/tags/Lei.html" title=类>类构造函数之前调用。

Program 3:

class="raw_b_node">程式3:

class="has">#include <iostream>
#include <string.h>
using namespace std;

class Class1 {
public:
    Class1()
    {
        cout << "Class1 ctor called " << endl;
    }
};

class Class2 : public Class1 {
public:
    Class2()
    {
        cout << "Class2 ctor called " << endl;
    }
};

typedef class Class3 : public Class2 {
public:
    Class3()
    {
        cout << "Class3 ctor called " << endl;
    }
} DerivedClass;

int main()
{
    DerivedClass* PTR;
    PTR = new DerivedClass();
    return 0;
}

Output:

class="raw_b_node">输出:

class="has">Class1 ctor called
Class2 ctor called
Class3 ctor called

Explanation:

class="raw_b_node">说明:

Here, in the above program, we created three classes Class1, Class2, and Class3, each of them are containing a default constructor.

在这里,在上面的程序中,我们创建了三个class="tags" href="/tags/Lei.html" title=类>类Class1Class2Class3 ,每个class="tags" href="/tags/Lei.html" title=类>类都包含一个默认构造函数。

Here, Class1 is inherited by Class2 and Class2 inherited by Class2. We also created alias using the typedef of Class3 that is Derived class. This type of inheritance is known as multi-level inheritance.

在这里,1class="tags" href="/tags/Lei.html" title=类>类是继承的Class2Class2中继承了Class2中 。 我们还使用Class3typedef (即派生class="tags" href="/tags/Lei.html" title=类>类)创建了别名。 这种class="tags" href="/tags/Lei.html" title=类>类型的继承称为多级继承。

In the main() function, we created the pointer of DerivedClass and pointer of the DerivedClass initialized using the new operator.

main()函数中,我们创建了DerivedClass的指针和使用new运算符初始化的DerivedClass的指针。

Then, Class1 constructor called before Class2 and Class2 constructor called before Class3.

然后,在Class2之前调用Class1构造函数,在Class3之前调用Class2构造函数。

翻译自: https://www.includehelp.com/cpp-tutorial/inheritance-find-output-programs-set-2.aspx

c ++ 继承


http://www.niftyadmin.cn/n/1255575.html

相关文章

重学数据结构:链栈的各种操作(C语言)

文章目录链栈的定义头插法建立链表(带头节点)打印链栈压栈退栈测试代码测试结果链栈的定义 其实跟链表的结构是一样的&#xff0c;连同操作基本一样&#xff0c;但是链栈只能在一端进行删除节点和增加节点. typedef struct LNode {struct LNode *next;int data;int maxsize; …

开源软件和自由软件_自由和开源软件中的机会

开源软件和自由软件Other potential methods for getting cash from Free and Open Source include, 从免费和开源中获取现金的其他潜在方法包括&#xff1a; Dual License model, when similar code is given both under forceful Free and Open Source License (normally GP…

c ++ 继承_C ++继承| 查找输出程序| 套装4

c 继承Program 1: 程序1&#xff1a; #include <iostream>#include <string.h>using namespace std;class Class1 {protected:void fun1(){cout << "Fun1() called" << endl;}};class Class2 : protected Class1 {protected:void fun2(){c…

天台人满为患,不如来看下这个Ramnit蠕虫DesktopLayer.exe分析

今年的世界杯越来越看不懂&#xff0c;想去天台吹吹风都不一定有位置&#xff0c;心凉了&#xff0c;事儿还得做&#xff0c;先从网上抓个可疑样本压压惊&#xff01;上手分析才发现并没有我想得那么简单…… 一、基本信息 MD5ff5e1f27193ce51eec318714ef038bef 文件大小 55 KB…

开源软件和自由软件_开源软件–四个自由度

开源软件和自由软件开源软件中的四个基本自由 (Four Essential Freedoms in Open Source Software) Software is said to be free if the Softwares User has some Basic Freedoms while using it. 如果软件的用户在使用软件时具有一些基本自由&#xff0c;则称该软件是免费的。…

BZOJ2534 Uva10829L-gap字符串 字符串 SA ST表

原文链接https://www.cnblogs.com/zhouzhendong/p/9240665.html 题目传送门 - BZOJ2534 题意 有一种形如 $uvu$ 形式的字符串,其中 $u$ 是非空字符串,且 $V$ 的长度正好为 $L$ ,那么称这个字符串为 $L-Gap$ 字符串。 给出一个字符串 $S$ ,以及一个正整数 $L$ ,问 $S$ 中有多少个…

重学数据结构:队列的顺序实现的各种操作(C语言)

文章目录队列的顺序实现定义队列初始化判断队满判断队空创建队列打印队列入队退队测试代码测试结果队列的顺序实现定义 队列的定义和顺序表、顺序栈其实差不多&#xff0c;只不过有两个变量来记录队列头&#xff0c;尾&#xff0c;操作不尽相同&#xff0c;队列只能在尾部插入…

luogu P1914小书童——密码

小书童——密码题面 总是60分&#xff0c;不给过&#xff0c;下了一个数据测试点看了看原来密码中还会有数字&#xff0c;可题面里明明说是全部小写字母的&#xff01;莫非&#xff0c;我下载了个假的测试点啊啊啊啊( ⊙ o ⊙ )啊&#xff01; 先看看我60分的完美代码吧︿(&…