Inheritance of C ++ class

Source: Internet
Author: User
Tags export class

1. class inheritance

Syntax: class export class Name: Inheritance Method Base class Name 1,..., inheritance method base class name n class body

There are three inheritance Methods: public, protected, and private.

Composition of export class objects: (1) Objects of the base class; (2) Objects of the export class.

The following table lists the access permissions of the members in the export class.

Inheritance Method Base class private member Base class protected member Public Member of the base class
Private Private Private Private
Protected Private Protected Protected
Public Private Protected Public

Test procedure:

# Include "stdafx. h"
Class Base
{
Private:
Int b1;
Protected:
Int b2;
Public:
Int b3;
};

Class D: public Base
{
Private:
Int d1;
Protected:
Int d2;
Public:
Int d3;
};

Class E: private D
{
Private:
Int e1;
Protected:
Int e2;
Public:
Int e3;
};

Void main ()
{
Base B;
// Error C2248: 'base: b1': cannot access private member declared in class 'base'
// B. b1 = 1;
// Error C2248: 'base: b2': cannot access protected member declared in class 'base'
// B. b2 = 2;
B. b3 = 3;

D;
// Error C2248: 'base: b1': cannot access private member declared in class 'base'
// D. b1 = 1;
// Error C2248: 'base: b2': cannot access protected member declared in class 'base
// D. b2 = 2;
D. b3 = 3;
// Error C2248: 'd: d1 ': cannot access private member declared in class 'D'
// D. d1 = 4;
// Error C2248: 'd: d2 ': cannot access protected member declared in class 'D'
// D. d2 = 5;
D. d3 = 6;

E e;
// Error C2248: 'base: b1': cannot access private member declared in class 'base'
// E. b1 = 1;
// Error C2248: 'base: b2': cannot access protected member declared in class 'base'
// E. b2 = 1;
// Error C2247: 'base: b3' not accessible because 'E' uses 'private' to inherit from 'D'
// E. b3 = 1;
// Error C2248: 'd: d1 ': cannot access private member declared in class 'D'
// E. d1 = 1;
// Error C2248: 'd: d2 ': cannot access protected member declared in class 'D'
// E. d2 = 1;
// Error C2247: 'd: d3 'not accessible because 'E' uses 'private' to inherit from 'D'
// E. d3 = 1;
// Error C2248: 'e: e1 ': cannot access private member declared in class 'E'
// E. e1 = 1;
// Error C2248: 'e: e2': cannot access protected member declared in class 'E'
// E. e2 = 1;
E. e3 = 1;

}

Note: Only public members of the callback class are allowed outside the class.

2. Do not inherit

(1) constructor

(2) destructor

(3) operator =

Export class constructor definition:

Constructor (form parameter table): The member initializes the table function body.

Member initialization table: Base Class Name (real parameter table), data member name (real parameter)

{

Assign initial values to common members;

}

Constructor call sequence:

The first base class (the sequence when the class is inherited) and the object members (the sequence is declared in the class body ).

The order of destructor calls is the opposite.

3. Virtual Functions

(1) upward Transformation

For example:

# Include "stdafx. h"
Class Base
{
};

Class Derived: public Base
{
};

Void main ()
{
Base B, * p;
Derived d, * q;
P = & B;

// The pointer to the base class can point to the subclass, and vice versa.
P = & d;

Q = & d;
Q = (Derived *) & B;
}

Note: q = (Derived *) & B; is mandatory conversion in C language. The newly introduced cast operation should be used in C ++.

(2) binding)

The process of determining the function body by calling the function.

Static binding: Binding during compilation;

Dynamic binding: binding at runtime.

(3) virtual functions

Definition Syntax:

Virtual Value Type Function Name (form parameter table) function body

Note:

A. virtual does not need to be written when defining classes in vitro.

B. In the export class, virtual functions are not allowed to be written when the override virtual function is used, but the function prototype must be the same.

Feature: When you call a virtual function using a pointer or reference, the specific function called is determined by the type of the object indicated by the pointer.

Example:

# Include "stdafx. h"
# Include <iostream>
Using namespace std;
Class Base
{
Public:
Virtual void f ()
{
Cout <"f in Base" <endl;
}
Void g ()
{
Cout <"g in Base" <endl;
}
};

Class Derived: public Base
{
Public:
Void f ()
{
Cout <"f in Derived" <endl;
}
Void g ()
{
Cout <"g in Derived" <endl;
}
};

Void main ()
{
Base * p = new Derived ();
P-> f ();
P-> g ();

Base * q = new Base ();
Q-> f ();
Q-> g ();
}

(4) pure virtual functions: only declared, non-defined virtual functions

Statement Syntax:

Virtual Value Type Function Name (parameter table) = 0;

Abstract class: classes that contain at least one pure virtual function. abstract classes cannot be instantiated, that is, objects cannot be defined.

# Include "stdafx. h"

# Include <iostream>
Using namespace std;

# Include "string. h"

Class List;

Class Node
{
Private:
Node * next;

Public:
Virtual void show () = 0;
Void insert (Node ** h)
{
Next = * h;
* H = this;
}

Friend class List;
};

Class iNode: public Node
{
Int info;

Public:
INode (int x)
{
Info = x;
}

Void show ()
{
Cout <info <"";
}
};

Class sNode: public Node
{
Char s [100];

Public:
SNode (char * t)
{
Strcpy (s, t );
}

Void show ()
{
Cout <s <"";
}
};

Class List
{
Node * head;
Public:
List ()
{
Head = NULL;
}

Void ins (Node * p)
{
P-> insert (& head );
}

Void display ()
{
Node * p = head;
While (p)
{
P-> show ();
P = p-> next;
}
}
};

Void main ()
{
List list;

List. ins (new iNode (1 ));
List. ins (new iNode (2 ));
List. ins (new iNode (3 ));
List. ins (new iNode (4 ));
List. ins (new iNode (5 ));
List. ins (new sNode ("ABC "));
List. ins (new sNode ("B "));
List. ins (new sNode (""));

List. display ();
}


Note: the usage of virtual destructor is omitted.

<Supplement>

Void insert (Node ** h)
{
Next = * h;
* H = this;
Show ();
Cout <endl;
}

After the show () method is added, the compilation will not fail, and then the subclass method will be called.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.