#include<iostream> using namespace std; int Add(int a,int b) { return a+b; } float Add(float a,float b) { return a+b; } int main() { cout<<Add(4,5)<<endl;//調用 int Add(int a,int b) cout<<Add(2.5f,3.7f)<<endl;//調用 float Add(float a,float b) return 0; } |
#include<iostream> using namespace std; class Person//父類 { public: virtual void BuyTickets()//父類虛函數 { cout<<" 買票-全票"<< endl; } protected : string _name; // 姓名 }; class Student : public Person//子類 { public: void BuyTickets()//子類虛函數 { cout<<" 買票-半價"<<endl ; } protected : int _num ; //學號 }; void Fun (Person* p) { p->BuyTickets(); } void Fun (Person&p) { p.BuyTickets(); } void Test () { Person p ; Student s; Fun(p); Fun(s); Fun(&p); Fun(&s); } int main() { Test(); return 0; } |
#include<iostream> using namespace std; class A { protected: A(int x=2) :_a(x) {} //public: // //1 // void show() // { // cout<<"A::shou()"<<endl; // } //public: // //2 // virtual void show() // { // cout<<"A::shou()"<<endl; // } public: //3 void show(int a) { cout<<"A::shou()"<<endl; } public: int _a; }; class B:public A { public: B(int x=1) :_a(x) {} //public: // //1 // void show(int b) // { // cout<<"B::shou()"<<endl; // } //public: // //2 // void show(int a) // { // cout<<"B::shou()"<<endl; // } public: //3 void show(int a,int b) { cout<<"B::shou()"<<endl; cout<<_a<<endl; } public: int _a; }; int main() { B b; cout<<(b._a)<<endl; b.show(1); return 0; } |
| 歡迎光臨 (http://www.raoushi.com/bbs/) | Powered by Discuz! X3.1 |