C ++ abstract class instance code

Source: Internet
Author: User

1. All classes that contain pure virtual functions are abstract classes;
2. abstract classes cannot be instantiated;
3. You can use the public method to inherit the abstract class and implement all pure virtual functions in the abstract class;

Example code:
Myclass. h
Namespace myclass
{
Class MyClass
{
Public:
MyClass (){};
Static int myget ();
Virtual int func1 () = 0;
Virtual int func2 () = 0;
Static void myset (char **);
~ MyClass (){};
};
Class NewClass: public MyClass
{
Public:
NewClass (){};
Virtual int func1 ();
Virtual int func2 ();
~ NewClass (){};
};
}
 
Myclass. cpp
# Include <iostream>
# Include "myclass. h"
Int myclass: MyClass: myget ()
{
Return 111;
}
Void myclass: MyClass: myset (char ** p)
{
* P = (char *) malloc (10 );
Char * t = "good ";
Strcpy (* p, t );
Return;
}
Int myclass: NewClass: func1 ()
{
Return 1;
}
Int myclass: NewClass: func2 ()
{
Return 2;
}
 
Test. cpp
# Include <iostream>
# Include "myclass. h"
Using namespace std;
Using namespace myclass;
Int main ()
{
Int x = MyClass: myget ();
Cout <"x =" <x <endl;
Char * p;
MyClass: myset (& p );
Cout <"p =" <p <endl;
Free (p );
NewClass * oneclass = new NewClass ();
MyClass * twoclass = new NewClass ();
Cout <"twoclass-> myget () =" <twoclass-> myget () <endl;
Return 0;
}

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.