#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.51hei.com/bbs/) | Powered by Discuz! X3.1 |