1 //++++++++++++++++++++++++++
2 // HelloClass.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include header
6 #include <iostream>
7 #include <string>
8
9 ///Using
10 using namespace std;
11
12 ///Class Cellphone
13 class Cellphone //function is method , variable is member
14 {
15 ///Public
16 public:
17 string brand = "APPLE";
18 void outlook() { cout << "cool ! \n" ; }
19 void display() { cout << "shine ! \n" ; }
20
21 ///+
22 }; //end class include semi colon
23
24 ///Class Smartphone
25 class Smartphone: public Cellphone // colon : symbol to inherit from a class.
26 {
27 ///Public
28 public:
29 string model = "iPhoneX";
30 void display() { cout << "glitter! \n" ; } //Polymorphism
31
32 ///+
33 }; //end class include semi colon
34
35 ///mainentry
36 int main()
37 {
38 ///declare
39 Smartphone myPhone;
40
41 ///myphone .outlook
42 myPhone.outlook();//inherit from Cellphone.outlook()
43
44 ///myphone .display
45 myPhone.display();//inherit from Cellphone.display() , Smartphone.display() override former
46
47 ///print data
48 cout << myPhone.brand + " " + myPhone.model;
49
50 ///mainend
51 return 0;
52 } //main_end
53
This Blog is dedicated to Sir Johnson Lin
C++ 基本 使用類別多樣 C++ basic function ,class Polymorphism introduction
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言