Derivation and private protection

Source: Internet
Author: User

[Cpp]
# Include <iostream>
Using namespace std;
Enum BREED {black, red, white, blue };
 
Class mam {
Public:
Mam (): age (4), weight (5 ){};
~ Mam (){}
Int get_age () const {return age ;}
Void set_age (int age1) {age = age1 ;}
Int get_weight () const {return weight ;}
Void set_weight (int x) {weight = x ;}
Void speak () const {cout <"mam sound! \ N ";}
Void sleep () const {cout <"I 'am sleeping. \ n ";}
Protected:
Int age;
Int weight;
};
 
Class dog: public mam {
Public:
Dog (): its_breed (red ){}
~ Dog (){}
BREED get_breed () const {return its_breed ;}
Void set_breed (BREED breed) {its_breed = breed ;}
Void wag_tail () const {cout <"Tail wagging... \ n ";}
Void beg_for_food () const {cout <"begging for food... \ n ";}
Private:
BREED its_breed;
};
 
Int main ()
{
Dog fido;
Fido. speak ();
Fido. wag_tail ();
Cout <"fido is" <fido. get_age () <"years old" <endl;
Cout <fido. get_breed () <endl;
Fido. set_breed (white );
Cout <fido. get_breed () <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.