C + + Learning notes

Source: Internet
Author: User

encapsulation and inheritance are intended to enable the reuse of code,and polymorphism is to realize the reuse of the interface. sizeof (class) is asking forafter the class member hasAddvirtual function table headerAnd if the inheriting class virtual function only makes one table, then there is only one header operator overload code example #include<iostream>using namespace Std;class point {private:int x;PublicPoint (int x1){ x=x1;} Point (point& p){ x=p.x;} Const Point operator+ (const point& p);//Use the member function to overload the plus operatorFriend Const Point operator-(const point& p1,const point& p2);//Use the friend function overload minus operator}; Const point point::operator+ (const point& p) {return point (x+p.x);} Point const operator-(const point& p1,const point& p2) {return point (p1.x-p2.x);}int Main () {return 0;} Inherited code example//example # include <iostream>using namespace std; #include <string>class animal{string name;Publicvirtual void eat () =0;//must not be executed, pure virtual functionvirtual void sleep () {cout << "Animal Rest" << Endl;}virtual void shout () {cout << "animals called" << Endl;}};//Note the semicolon class Cat:public Animal{public:virtual//virtual writable */void Eat () {cout << "Cat write cat food" << Endl;}void Sleep () {cout << "Cat sleeps" << Endl;}void Shout () {cout << "cat meow Meow" << Endl;}};//Note the semicolon class Dog:public Animal{public:void Eat () {cout << "Dog eats Bones" << Endl;}void Sleep () {cout << "dogs in bed" << Endl;}void Shout () {cout << "My name wong Choy" << Endl;}};//semicolons cannot be less than class Jiafei:public Cat{public:void Eat () {cout << "plus non-cat love pasta" << Endl;}void Sleep () {cout << "plus non-cats sleeping on the couch" << Endl;}void Shout () {cout << "plus non-cat say good afternoon" << Endl;}}; Semicolon Class player{string name;Public:Player (string n): Name (n) {}void Play (animal* p/* pointer */) {p->eat ();p->sleep ();p->shout ();}void Play (animal& p/*/ref */) {p.eat ();p.sleep ();p.shout ();}};//Semicolontypedef void (*fun) (void); int main () {//cout << sizeof (Animal) << Endl;//Output 8Cat C;Dog D;Jiafei J;Player P1 ("small");Player P2 ("Jolin Tsai");P1.play (&c);P2.play (&d);P2.play (&j); Cat *pc=&c;Dog *pd=&d;cout<< (int*) pc<<endl;cout<<sizeof (* (int*) PC) <<endl;cout<<sizeof (Animal) <<endl;cout<<sizeof () <<endl;return 0;} Virtual function table Code example # include <iostream.h> typedef void (*fun) (void); Class Base {Public :virtual void F () {cout<< "Base::f" <<ENDL;}virtual void g () {cout<< "Base::g" <<ENDL;}virtual void H () {cout<< "Base::h" <<ENDL;}};int Main () {Base B; Fun Pfun = NULL; cout<< "virtual function table Address:" << (int*) (&b) <<endl; cout<< "virtual function table-First function address:" << (int*) * (int*) (&b) <<endl; Pfun = (Fun) * ((int*) * (int*) (&b)); Pfun (); Pfun = (Fun) * (1+ (int*) * (int*) (&b)); Pfun (); Pfun = (Fun) * (int*) * (int*) (&b)); Pfun (); return 0; }

C + + Learning notes

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.