C++ 類 (Class Inheriance) : 料 ( 異 ), 類 料, 利 類 狀, 不 更 來 data membermember function 類 料 類 類 (Base class) 類, 類 類 (Derived class) 類 利 料 料, 料 不 料, 料 料, 留 類 類, 料, 類 來 類 列 更 類 數 例 : 類 數 類 料 例, String 類, 類 料, 串 類 類 類 不,, 類 類 類 P. 1 C++ 類 (Class Inheritance)
Class 類 : {... class class shape shape { { x,y; x,y; draw() draw() {. {. s1; s1; class class circle: circle: public public shape shape { { private: private: radius; radius; draw() draw() { { c1; c1; main() main() { { public privaite c1 s1 x y radius manager employee name 類 number title Class Class employee employee { { private: private: char char name[20]; name[20]; unsigned unsigned long long number; number; getdata() getdata() {cout {cout << << \n \n Name: ; Name: ; cin cin >>name; >>name; cout cout << << \n \n Number; ; Number; ; cin cin >> >> number; number; putdata() putdata() { { cout cout << << \n \n Name: Name: << << name; name; cout cout << << \n \n Number: Number: <<number; <<number; class class manager: manager: private private employee employee { { private: private: har har title[30]; title[30]; getdata() getdata() { { putdata() putdata() { { main() main() { { P. 2 C++ 類 (Class Inheritance)
類 Class Base a; func1(){ b; func2(){ c; func3(){ main() { datafunction 類 datafunction 類 datafunction 數 func1(){a=2; func1(){a=2; func2(){a=3; func2(){a=3; func1(); func1(); func3(){a=3; func3(){a=3; func1(); func1(); func2(){b=2; func2(){b=2; func3(){a=3; func3(){a=3; func1(); func1(); func3(){c=3; func3(){c=3; func1(); func1(); main() main() {{ base base bs1; bs1; bs1.c=3; bs1.c=3; bs1.func3(); bs1.func3(); main() main() {a=2; {a=2; func1(); func1(); main() main() {b=2; {b=2; main() main() {base {base bs1; bs1; c=2; c=2; func3(); func3(); P. 3 C++ 類 (Class Inheritance)
Class Base a; func1(){ b; func2(){ c; func3(){ b1; class Drbase:public base d; func4(){ e; func5(){ f; func6(){ datafunction 類 datafunction 類 datafunction 數 Drbase 類 public, 類 protected public data member member function protected public : : a; a; c; c; func1(){ func1(){ f; f; private: private: func3(){ func3(){ d; d; func6(){ func6(){ func4(){ func4(){ b; b; e; e; func2(){ func2(){ func59){ func59){ P. 4 C++ 類 (Class Inheritance)
Class Base a; func1(){ b; func2(){ c; func3(){ b1; class Drbase:private base d; func4(){ e; func5(){ f; func6(){ datafunction 類 datafunction 類 datafunction 數 Drbase 類 private, 類 protected public data member member function private : : a; a; e; e; func1(){ func1(){ func5(){ func5(){ private: private: b; b; f; f; c; c; func6(){ func6(){ d; d; func2(){ func2(){ func3(){ func3(){ func4(){ func4(){ P. 5 C++ 類 (Class Inheritance)
, 類 類 data member member function public private 兩 : 若 public, 類 類 protected public data member member function protected public 若 private, 類 類 protected public data member member function private public : 類 public 料 類 public 料 類 protected 料 類 protected 料 類 private 料 類, 類 protected public 數 private : 類 public 料 類 private 料, 類 member function 類 protected 料 類 private 料, 類 member function 類 private 料 類, 類 protected public 數 P. 6 C++ 類 (Class Inheritance)
念 #include <iostream.h> class Counter { unsigned count; Counter(){ count=0; Counter( c){ count=c; get_count(){ return count; Counter operator ++ () { count++; return Counter(count); class CountDn:public Counter { Counter operator -- () { count--; return Counter(count); main() { CountDn c1; cout << "\nc1=" << c1.get_count(); c1++;c1++;c1++; cout << "\nc1=" << c1.get_count(); c1--;c1--; cout << "\nc1=" << c1.get_count(); CountDn 類 Private: Private: unsigned unsigned count; count; Counter(){ Counter(){ Counter( Counter( c){ c){ get_count(){.. get_count(){.. Counter Counter operator operator ++ ++ () (){ Counter Counter operator operator -- --()(){ Ans: c1=0 c1=3 c1=1 P. 7 C++ 類 (Class Inheritance)
念 #include <iostream.h> class incount { c1,c2; incount() { c1=0; c2=1000; incount( vc1, vc2) { c1=vc1; c2=vc2; retcount() { cout << "c1=" << c1; cout << " c2=" << c2 << endl; incount operator ++ () { c1++; c2++; return incount(c1,c2); class dncount:public incount { incount operator -- () { c1--; c2--; return incount(c1,c2); main() { dncount dc1,dc2; dc1.retcount(); dc2.retcount(); dc1--; dc1.retcount(); dc2++; dc2.retcount(); dncount 類 Private: Private: c1,c2; incount(){ incount(){ incount( incount( vc1, vc1, vc2){ vc2){ retcount(){ retcount(){ incount incount operator operator ++ ++ () (){ incount incount operator operator -- --()(){ Ans: c1=0 c2=1000 c1=0 c2=1000 c1=-1 c2=999 c1=1 c2=1001 P. 8 C++ 類 (Class Inheritance)
Private Public #include <iostream.h> class basescore math; eng; chem; class dbase1: public basescore { input() { // cout << "Input Math:"; cin >> math; error! cout << "Input English:"; cin >> eng; cout << "Input Chemtry:"; cin >> chem; // class dbase2: protected basescore{ class dbase3: private basescore { input() { //cout << "Input Math:"; cin >> math; cout << "Input English:"; cin >> eng; cout << "Input Chemtry:"; cin >> chem; main() { dbase1 pub; pub.input(); dbase3 pri; pri.input(); s,t; // s=pub.math; // s=pub.eng; s=pub.chem; // t=pri.math; // t=pri.eng; // t=pri.chem; dbase1 類 : : math; math; Private: Private: eng; eng; chem; chem; input(){ input(){ dbase3 類 : : math; Private: Private: eng; eng; chem; chem; input(){ input(){ P. 9 C++ 類 (Class Inheritance)
類 數 立 類, 類 數, 類 數, 類 數, 類 數 #include <iostream.h> class Base { Base() { cout << "in Base's constructor\n"; ~Base() { cout << "in Base's destructor\n"; class DRBase:Base { DRBase() { cout << "in DRBase's constructor\n"; ~DRBase() { cout << "in DRBase's destructor\n"; main() {Base b1,b2; DRBase dr1,dr2; Output: in Base s constructor //construct b1 in Base s constructor //construct b2 in Base s constructor /construct dr1 in DRBase s constructor //construct dr1 in Base s constructor //construct dr2 in DRBaase s constructor //construct dr2 in DRBase s destructor //destruct dr2 in Base s destructor //destruct dr2 in DRBase s destructor //destruct dr1 in Base s destructor //destruct dr1 in Base s destructor //destruct b2 in Base s destructor //destruct b2 P. 10 C++ 類 (Class Inheritance)
#include <iostream.h> #include <conio.h> class Base {private: a; Base() { a=1; cout << "a=" << a << endl; Base( c) { a=c; cout << "a=" << a << endl; ~Base() { cout << "in Base's destructor\n"; Output: a=1 a=3 a=1 b=1 a=1 b=5 in DRBase s destructor in Base s destructor class DRBase:Base b; DRBase() { b=1; cout << "b=" << b << endl; DRBase( d) { b=d; cout << "b=" << b << endl; ~DRBase() { cout << "in DRBase's destructor\n"; main() {clrscr(); Base b1,b2(3); DRBase dr1,dr2(5); in DRBase s destructor in Base s destructor in Base s destructor in Base s destructor P. 11 C++ 類 (Class Inheritance)
類 數 類 數 class incount { c1,c2; incount(){c1=0; c2=1000; incount( vc1, vc2) {c1=vc1; c2=vc2; class dncount : public incount { dncount():incount() {... dncount( a, b): incount(a,b) { incount 類 incount 類 incount 類, 參 數 類 incount 類, 類 若 行 P. 12 C++ 類 (Class Inheritance)
#include <iostream.h> class incount // Page;13-15 { c1,c2; incount() {c1=0;c2=1000; incount( vc1, vc2) { c1=vc1; c2=vc2; retcount() { cout << "c1=" << c1 << " c2=" << c2 << endl; incount operator ++ () { c1++; c2++; return incount(c1,c2); class dncount: public incount { dncount(): incount() { dncount( a, b):incount(a,b) { dncount operator -- () { c1--; c2--; return dncount(c1,c2); main() { dncount dc1,dc3; dncount dc2(200,500); dc1.retcount(); dc2.retcount(); dc1--; dc1.retcount(); dc2--; dc2--.retcount(); dc3=dc1--; dc3--.retcount(); Output: c1=0 c2=1000 c1=200 c2=500 c1=-1 c2=999 c1=198 c2=498 c1=-3 c2=997 P. 13 C++ 類 (Class Inheritance)
類 member function 類 member function, member function 類 來 行 類 member fucntion #include <iostream.h> #include <process.h> const max=500; class queue { q[max]; rear, front; queue() { rear=front=0; cout << "queue initialization \n"; qinsert( i) { rear++; q[rear]=i; cout << "rear=" << rear << endl; qdelete() { front ++; cout << "front=" << front << endl; return q[front]; class queue2 : public queue { qinsert( i) { if (rear<max) queue::qinsert(i); else cout << "queue is full \n"; qdelete() { if (front<rear) return queue::qdelete(); else cout << "queue underflow \n"; main() { queue2 a; a.qinsert(199); a.qinsert(200); cout << "1: " << a.qdelete() << endl; cout << "2: " << a.qdelete() << endl; cout << "3: " << a.qdelete() << endl; front rear front... 199 200... rear Output: queue initialization rear=1 rear=2 front=1 1:199 front=2 2:200 queue underflow P. 14 C++ 類 (Class Inheritance)
類 類 料, 類 類 類 料 #include <iostream.h> class incount { c1,c2; incount() { c1=0; c2=1000; incount( vc1, vc2) { c1=vc1; c2=vc2; retcount() { cout << "c1=" << c1 << " c2=" << c2; incount operator ++() { c1++; c2++; return incount(c1,c2); class dncount: public incount c3; dncount():incount() { c3=10000; dncount( vc1, vc2, vc3) :incount(vc1,vc2) {c3=vc3; dncount operator -- () { c1--; c2--; c3--; return dncount(c1,c2,c3); retcount() { incount::retcount(); cout << "c3=" << c3 << endl; main() { dncount dc1,dc2; dc1.retcount(); dc2.retcount(); dc1--; dc1.retcount(); dc2=dc1--; dc2--; dc2.retcount(); dncount dc3(-100,10,10000); dc3--; dc3--.retcount(); Output: c1=0 c2=1000 c3=10000 c1=0 c2=1000 c3=10000 c1=-1 c2=999 c3=9999 c1=-3 c2=997 c3=9997 c1=-102 c2=8 c3=9998 P. 15 C++ 類 (Class Inheritance)
類 類 data member member function, 類 更 #include <iostream.h> //Page:13-25 #include <iomanip.h> const L=80; class person char name[l], id[l]; input() { cout << "\n Input name:"; cin >> name; cout << " ID No:"; cin >> id; pr() { cout << "\n ID No is:" << id; prname() { cout << setw(8) << name; class student: public person char tel[l]; unsigned long sno; input() { person::input(); cout << " Input Tel No:"; cin >> tel; cout << "Input Score no:"; cin >> sno; pr() { person::pr(); cout << "\n Tel:" << "\t" << tel; cout << "\n Score No:" << "\t" << sno; // prname() // { person::prname(); P. 16 C++ 類 (Class Inheritance)
class teacher : public person char degree[l], dep[l]; input() { person:: input(); cout << "Input Degree:"; cin>>degree; cout << "Input Department:"; cin>>dep; pr() { person::pr(); cout << "\n Degree: " << "\t << degree; cout << "\n Department: << "\t" << dep; // prname() // { person::prname(); student tel tel scoreno scoreno pr() pr() main() { student s1; teacher t1; cout << "Input data for student 1:"; s1.input(); cout << "Input data for teacher 1:"; t1.input(); cout << "Data on teacher:"; t1.prname(); t1.pr(); cout << "Data on student:"; s1.prname(); s1.pr(); person name name id id pr() pr() prname() prname() teacher degree degree department department pr() pr() P. 17 C++ 類 (Class Inheritance)
#include <iostream.h> // Page:13-29 #include <iomanip.h> const L=80; class person char name[l], id[l]; input() { cout << "\n Input name:"; cin >> name; cout << " ID No:"; cin >> id; pr() { cout << "\n ID No is:" << id; prname() { cout << setw(8) << name; class student: public person char tel[l]; unsigned long sno; input() { person::input(); cout << " Input Tel No:"; cin >> tel; cout << "Input Score no:"; cin >> sno; pr() { person::pr(); cout << "\n Tel:" << "\t" << tel; cout << "\n Score No:" << "\t" << sno; // prname() // { person::prname(); P. 18 C++ 類 (Class Inheritance)
class teacher : public person char degree[l], dep[l]; input() { person:: input(); cout << "Input Degree:"; cin>>degree; cout << "Input Department:"; cin>>dep; pr() { person::pr(); cout << "\n Degree: " << "\t" << degree; cout << "\n Department:" << "\t" << dep; class score: public student math; eng; input() { student::input(); cout << "Input Math score:"; cin >> math; cout << "Input Eng score:"; cin >> eng; pr() { student::pr(); cout << "\n Math:" << "\t" << math; cout << "\n Eng:" << "\t" << eng; cout << "\n Average:" << "\t" << float (math+eng)/2; student main() { teacher t1; score c1; cout << "Input data for teacher 1:"; t1.input(); cout << "Input data for score 1:"; c1.input(); cout << "Data on teacher:"; t1.prname(); t1.pr(); cout << "\n Data on student"; c1.prname(); c1.pr(); tel tel scoreno scoreno pr() pr() person name name id id pr() pr() prname() prname() score math math eng eng pr() pr() teacher degree degree department department pr() pr() P. 19 C++ 類 (Class Inheritance)
#include <iostream.h> #include <iomanip.h> const L=80; class person char name[l], id[l]; input() { cout << "\n Input name:"; cin >> name; cout << " ID No:"; cin >> id; pr() { cout << "\n ID No is:" << id; prname() { cout << setw(8) << name; class stud char addr[l]; old; input() { cout << " Input address:"; cin >> addr; cout << " \n Years old:"; cin >> old; pr() { cout << "\n Address:" << "\t" << addr; cout << "\n Years old:" << "\t" << old; class student: public person char tel[l]; unsigned long sno; input() { person::input(); cout << " Input Tel No:"; cin >> tel; cout << "Input Score no:"; cin >> sno; pr() { person::pr(); cout << "\n Tel:" << "\t" << tel; cout << "\n Score No:" << "\t" << sno; class score: private student, private stud math; eng; input() { student::input(); stud::input(); cout << "Input Math score:"; cin >> math; cout << "Input Eng score:"; cin >> eng; P. 20 C++ 類 (Class Inheritance)
pr() { student::pr(); stud::pr(); cout << "\n Math:" << "\t" << math; cout << "\n Eng:" << "\t" << eng; cout << "\n Average:" << "\t" << float (math+eng)/2; employee prname() {student::prname(); class employee: public person { main() { score c1; employee e1; cout << "Input data for score 1:"; c1.input(); cout << "Input data for employee 1:"; e1.input(); cout << "\n Data on student"; c1.prname(); c1.pr(); cout << "\n Data on employee:"; e1.prname(); e1.pr(); person Private: Private: name name id id Public: Public: public pr() pr() prname() prname() public student Private: Private: tel tel sno sno Public: Public: pr() pr() private stud Private: Private: addr addr old old Public: Public: pr() pr() private score Private: Private: math math eng eng Public: Public: pr() pr() prname() prname() P. 21 C++ 類 (Class Inheritance)