| |

VerySource

 Forgot password?
 Register
Search
View: 1036|Reply: 8

How to change this code

[Copy link]

3

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-7 15:40:01
| Show all posts |Read mode
#include <iostream.h>
class Base {
public: virtual void fun () = 0;
};
class Test: public Base {
public: virtual void fun () {cout << "test.fun =" << endl;}
};
void main () {
Base a;
Test * p; p =&a;
}

There is an error in this code, but I don't know how to change it, please brothers to help change it
Reply

Use magic Report

0

Threads

19

Posts

12.00

Credits

Newbie

Rank: 1

Credits
12.00

 China

Post time: 2020-1-7 20:09:01
| Show all posts
class Base {
public: virtual void fun () = 0;
};

Base a; // Virtual base class cannot be instantiated
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-7 20:45:01
| Show all posts
Base is an abstract class and cannot be instantiated, so Base a; will definitely go wrong and can be changed
Test p;
Base * a =&p;
Reply

Use magic Report

1

Threads

6

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-7 23:45:01
| Show all posts
Test test;
Base * p =&test;
p-> fun ();
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-13 15:27:01
| Show all posts
I said in detail:
A class containing pure virtual functions is a virtual base class and cannot be instantiated, so the Base a in your main function is wrong.It is also worth noting that pure virtual functions cannot be fixed in the base class. The body of the function must be overridden in the derived class.
You should use this program to verify the polymorphism of C ++ runtime, first define a pointer to the base class, and then point to the derived class to implement polymorphism.
Test a;
Base * p =&a;
p-> fun ();
Reply

Use magic Report

0

Threads

49

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 Invalid IP Address

Post time: 2020-1-14 19:45:01
| Show all posts
Alas, I don't know why everyone likes to use the term virtual base class, but something like
class Base {
public: virtual void fun () = 0;
};
Such a class containing pure virtual functions should be called an abstract class instead of a virtual base class. The so-called virtual base class, such as the following
class A {....};
class B: virtual public A {....};
class C: virtual public A {....};
Here, A is called the virtual base class of B and C, because B and C use "virtual inheritance" when inheriting A. This inheritance method is used to prevent "diamond multiple inheritance" when multiple inheritance is used. such as:
class D: public B, pubilc C {....};
At this time, "virtual inheritance" can ensure that D saves only one member variable of A.
Ha ha, just be serious about the issues discussed, do n’t blame everyone
Reply

Use magic Report

1

Threads

27

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-1-15 17:54:01
| Show all posts
Learn
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-18 09:36:01
| Show all posts
I learned a lot, thank you guys ~
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-1-18 20:45:01
| Show all posts
THANK YOU.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list