Level 2013 C + + 12th week (Spring) project--member's access attribute, multiple inheritance

Source: Internet
Author: User
Tags access properties

Course Home:http://blog.csdn.net/sxhelijian/article/details/11890759, complete teaching program and resources Link

The first part of the program read 1, reading procedures. Access properties for members in the analysis class
#include <iostream>using namespace Std;class A                   //a is the base class {public:    void F1 ();    int i;protected:    void F2 ();    int j;private:    int k;}; Class B:public A       //b is a common derived class {public:    void F3 ();p rotected:    int m;private:    int n;}; Class C:public B       //c is a common derived class of B {public:    void F4 ();p rivate:    int p;}; int main () {    A A1;              A1 is the object B of base class A    B1;              B1 is the object of the derived class B,    C C1;              C1 is the object of the derived class C,    return 0;
(1) in the main function. Whether you can use b1.i. B1.J and B1.K refer to the members of base class A in derived classes I, J K?
(2) Can members in a derived class B call the member functions F1 and F2 in the base class A?
(3) Can a member function in a derived class B refer to the data member I, J K, in base class A?
(4) Whether it can be used in the main function with c1.i, C1.J, C1.K, C1.M, C1.N, C1.P, the member of the base class A, J K, the member m of the derived class B, N, and the member p of the derived class C?
(5) Can I call F1, F2, F3, F4 member functions in the main function with C1.F1 (), C1.f2 (), c1.f3 () and c1.f4 ()?
(6) The member function of the derived class C F4 can call the member function F1 in the base class A, F2 and the member functions in the derived class F3?
2, reading procedures. Please analyze the access rights of all members within the scope of each category
#include <iostream>using namespace Std;class a{public:    void F1 ();p rotected:    void F2 ();p rivate:    int i;}; Class B:public A{public:    void F3 ();    int k;private:    int m;}; Class c:protected b{public:    void F4 ();p rotected:    int n;private:    int p;}; Class d:private C{public:    void F5 ();p rotected:    int q;private:    int r;}; int main () {    A A1;    B B1;    C C1;    D D1;    return 0;}

Part 2nd Practice Project "Project 1-the succession of giraffes to animals" understanding the access qualifier and inheritance of derived classes for members in a base class
Please write down your gaze in the following program, stating that the corresponding statement is syntactically correct and why. The demo sample is given in the first program, and the other locations are modeled as finished. At the time of the machine, can compile the program to verify, read the errors given in English prompts, and to understand.


(1) Under the Public succession mode

#include <iostream>using namespace Std;class Animal//Animal class {Public:animal () {} void Eat () {cout <& Lt    "Eat\n";    }protected:void Play () {cout << "play\n";    }private:void drink () {cout << "drink\n"; }};class giraffe:public Animal//Giraffe class {Public:giraffe () {} void Strechneck () {cout << "strech NE    CK \ n ";        }private:void take () {eat (); That's right.      Under public inheritance, the public members of the base class are visible to the derived class drink ();       _______________ Play ();      _______________}};int Main () {giraffe gir;        Defines the object of the derived class gir.eat (); That's right.       Under public inheritance, the public members of the base class are visible to the derived class object Gir.play ();      _______________ Gir.drink ();       _______________ Gir.take (); _______________ GIR. Strechneck ();    _______________ Animal ANI;        Ani.eat ();       _______________ Ani.play ();      _______________ Ani.drink ();       _______________ Ani.take (); Error. A member of a derived class is not the base class object (regardless of the access property)Visible ANI. Strechneck ();  _______________ return 0;}

(2) under the private succession mode
#include <iostream>using namespace Std;class animal{public:    Animal () {}    void Eat ()    {        cout < < "eat\n";    } Protected:    void Play ()    {        cout << "play\n";    } Private:    void drink ()    {        cout << "drink\n";    }}; Class Giraffe:private Animal{public:    giraffe () {}    void Strechneck ()    {        cout << "Strech neck \ n ";    }    void Take ()    {        eat ();     _______________        drink ();   _______________        Play ();    _______________    }};int Main () {    giraffe gir;    Gir.eat ();    _______________    Gir.play ();   _______________    Gir.drink ();  _______________    return 0;}

(3) Under the protected succession mode
#include <iostream>using namespace Std;class animal{public:    Animal () {}    void Eat ()    {        cout < < "eat\n";    } Protected:    void Play ()    {        cout << "play\n";    } Private:    void drink ()    {        cout << "drink\n";    }}; Class giraffe:protected Animal{public:    giraffe () {}    void Strechneck ()    {        cout << "Strech Neck \ n ";    }    void Take ()    {        eat ();    _______________        drink ();  _______________        Play ();   _______________    }};int Main () {    giraffe gir;    Gir.eat ();   _______________    Gir.play ();  _______________    Gir.drink ();//_______________    return 0;}

"Project 2-Teacher and cadre category" (11th, exercise 9) Defines teacher (teacher) class and Cadre (cadre) category respectively. A new class of Teacher_cadre (teacher and cadre) is derived from these two classes using multiple inheritance methods. Requirements:
(1) data members including name, age, gender, address, telephone, etc. are included in the two base classes.
(2) The data member title (title) is also included in the teacher class. Data member post (title) is also included in the Cadre class, and data member wages (payroll) is also included in the Teacher_cadre class.
(3) The name, age, gender, address, telephone and other data members in the two base classes are given the same name. When referencing these data members, specify the scope.
(4) Declare member functions in the class body. Define member functions outside of the class.
(5) In the member function of the derived class Teacher_cadre, call the display function in the teacher class, output name, age, gender, title, address, phone, and then use the cout statement to output the job and salary.




"Item 3-motorcycles Inherit bicycles and motor vehicles" in the definition of the following paragraph, the virtual base class of bicycle class is the vehicle class, the virtual base class of motor vehicle class is also the vehicle class. The base classes for motorcycles are bicycles and motor vehicles. All classes are public-inherited. What you see.

Download the executable file link motorcar.exe.

(1) According to the above types of relationship between the descriptive narrative, complete the following sections of the vacancy code.
(2) Implement the member functions declared in the program, note that the action in the corresponding action occurs when the conditions are not satisfied when the prompt should be given.


(3) Execute the procedure and enjoy the process of driving the motorcycle. (You can download the executable file Motorcar.exe, first perform the re-programming.) This motorcycle is very safe without having to apply for a driver's license. )
(4) in the report. Please write it in your own words. What is the problem solved by using the virtual base class?

#include <iostream> #include <conio.h> #include <windows.h>using namespace Std;enum Vehiclestaus {  rest, running}; Vehicle status: Parking, travel class vehicle//Vehicle class {Protected:int maxspeed;//max speed int currentspeed;//current speed int weight;//car weight vehicle Staus status; rest-parking status; running-travel status public:vehicle (int maxs, int w);  Constructor, when initially, the current speed is always 0 and in the parking state void start (); From rest state to running, the initial velocity is 1 void stop ();  By running state to rest, the current speed is less than 5 o'clock before agreeing to stop void speed_up (); Acceleration, called 1 times. Speed plus 1 void slow_down (); Slow down, call 1 times, speed minus 1. Speed is 0 o'clock, parking};class bicycle: _____ (1) _________//(1) The virtual base class of bicycle class is vehicle class {protected:double height;//car height public:bicycle (int maxs=1   0, int w=50, int h=0.7); Define constructors};class motorcar: ___ (2) __________//(2) The virtual base class of motor vehicle class is also the vehicle class {Protected:int seatnum;//seat number int passengernum;//Multiply   Number of guests public:motorcar (int maxs=150, int w=1500, int s=5, int p=1);   Defines the constructor void Addpassenger (int p=1); Add the passengers, overcrowding to refuse, when someone alighted, p is negative. Of course, there are at least 1 passengers (drivers) in the car. Only after the car stopped to be able to get up and down.

};class Motorcycle: ___ (3) _________//(3) The base class for motorcycle class is bicycle class and motor vehicle class {public:motorcycle (int maxs=90, int w=100, int s=3, I NT P=1, int h=0.7);//define constructor void Show (); Shows the execution status of the motorcycle};int main () {motorcycle m; BOOL End=false; while (!end) {cout<< "please operate: 1-Start 2-acceleration 3-deceleration 4-people get on the bus 5-someone gets off 6-stop 0-end" <<endl; Char keydown= _getch (); _getch () returns the character read on the keyboard switch (keydown) {case ' 1 ': cout<< "The selected operation is 1-start \ t"; M.start (); Break Case ' 2 ': cout<< "The selected operation is 2-acceleration \ t"; M.speed_up (); Break Case ' 3 ': cout<< "The selected operation is 3-deceleration \ t"; M.slow_down (); Break Case ' 4 ': cout<< "The selected operation is 4-someone get on the bus \ T"; M.addpassenger (); Break Case ' 5 ': cout<< "The selected operation is 5-someone gets off \ t"; M.addpassenger (-1); Break Case ' 6 ': cout<< "The selected operation is 6-stop \ T"; M.stop (); Break Case ' 0 ': end=true; Break } m.show (); cout<<endl; Sleep (200); To include header file <windows.h>} return 0;}


"Item 4" Date Time class
Defines a date class for date, with data members including year, month, day, SetDate (int y,int m,int D), and printdate () functions, respectively, for setting dates and displaying dates. Define a time class again. Data members include hours, minutes, and seconds. The settime (int h,int m,int s) and printtime () functions are used to set the time and display time, respectively. On this basis, we define a date time class timedate, make full use of the methods provided in the existing two classes, realize the setting and display of the date and time, implement the class. The following is the main function for testing and the results of the test.
int main () {    timedate dt_a,dt_b (2010,4,16,9,30,0);    cout<< "dt_a:";    Dt_a.printdate_time ();    cout<<endl;    cout<< "Dt_b:";    Dt_b.printdate_time ();    Dt_a.settime (20,00,00);    Dt_a.setdate (2008,8,7);    cout<<endl;    cout<< "Dt_after uptate:";    Dt_a.printdate_time ();    return 0;}




================= Helijian csdn Blog column ================= |== It Student Growth Guide Column column category folder (not regularly updated) ==| |== C + + Classroom Online Column The course teaching link (sub-course grade) ==| |== I wrote the book-"The reverse of the university-to the positive energy of IT students" ==| ===== the runway for it novices to take off. University ===== with students to enjoy happiness and passion


Level 2013 C + + 12th week (Spring) project--member's access attribute, multiple inheritance

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.