#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
/*
权限:public > protected > private;
对于 ? 继承:父类中>= ? 权限的会继承到 子类中的 ?权限中.
*/
class Person{
public:
int public_a;
void Go(){
cout<<" person Go!"<<endl;
}
protected:
int protected_a;
void Go_pro(){
cout<<" pro_person Go!"<<endl;
}
private:
int private_a;
void Go_pri(){
cout<<" pri_Person Go!"<<endl;
}
};
class Solder:public Person{
public:
void fuck(){
//Go_pri();//私有是继承不了的
Go_pro();//保护是可以继承的.
Go();
}
private:
void GG(){
//Go_pri();
}
};
class Son:public Solder{
public:
void Goto(){
//Go_pro();
}
};
class A{
public:
int a;
private:
int b;
};
class B:public A{
public:
int c;
};
int main(){
cout<<sizeof(A)<<" "<<sizeof(B)<<endl;// 8 , 12说明被私有被继承,但是无法访问。
return 0;
}
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
/*
基类和子类的成员同名会出现隐藏。无论参数是否相同,不会出现重载!
静态多态:重载。
动态多态多态:前提是有封装,继承
virtual不能修饰:普通函数(不是类成员), 静态函数, 构造函数, 内联(inline)函数
析构函数:子类执行然后执行父类。
用虚函数 才能实现覆盖(重写)
基类和子类的成员同名会出现隐藏。无论参数是否相同,不会出现重载!
一个空的类sizeof为 1
有虚函数的 为4 ,虚表在内存的开始。头部。
*/
class Person
{
public:
void GO(){
cout<<" Person GGOO"<<endl;
}
void go()
{
cout<<" Person go "<<endl;
}
void go(int c){
cout<<" Person go "<<c<<endl;
}
virtual void black(){
cout<<" Person black"<<endl;
}
~Person()
{
cout<<"~~~Person"<<endl;
};
};
class Soldier:public Person
{
public:
void go()
{
cout<<" Soldier go "<<endl;
}
virtual void black(){
cout<<" Soldier black"<<endl;
}
void Sgo(){
cout<<" Sgo"<<endl;
}
~Soldier()
{
cout<<"~~Soldier"<<endl;
};
};
void solution()
{
Soldier *p = new Soldier;
p->go();//出现隐藏 这里不是重写 输出:Soldier go
// p->go(1); 出现隐藏
p->Person::go(1);//隐藏的调用.
p->GO();
p->black();//输出:Soldier black
p->Sgo();//
cout<<" --------------- "<<endl;
//向上转型
Person *f = new Soldier;
f->go();//输出Person go 没有发生覆盖
f->go(1);//可以执行
f->black();//Soldier black被覆盖
//f->Sgo(); 和java一样 子类特有的函数不能访问.
delete p;
p=NULL;
}
class A{
virtual void Fuck();
virtual void B();
};
int main()
{
cout<<sizeof(A)<<endl;
return 0;
}
运算符重载:
#include <iostream>
using namespace std;
/**
一元运算重载"-,++,
成员函数重载:
友元函数重载:
二元运算重载, x+x,<<,[]
成员函数;默认第一个为this指针
*/
class Coor
{
public:
Coor() {};
Coor(int x_,int y_):x(x_),y(y_)
{
}
Coor & operator -();
friend Coor& operator-(Coor &c);
//前置++
Coor& operator++();
//后置++
Coor operator++(int);
//------------二元
Coor operator+(const Coor&c2);
//friend Coor operator+(const Coor&c1,const Coor&c2); 友元方法
//输出 : 必须要友元
friend ostream& operator<<(ostream &output,Coor &c);
//引索 必须成员函数重载
void Print();
private:
int x;
int y;
};
Coor &Coor::operator-()
{
x=-x;
y=-y;
return *this;
}
Coor& operator-(Coor& c)
{
c.x = -c.x;
c.y = -c.y;
return c;
}
Coor& Coor::operator++()
{
x++;
y++;
return *this;
}
Coor Coor::operator++(int)
{
Coor old(*this);
this->x++;
this->y++;
return old;
}
Coor Coor::operator+(const Coor&c2)
{
Coor tmp;
tmp.x = this->x + c2.x;
tmp.y = this->y + c2.y;
return tmp;
}
ostream& operator<<(ostream &output,Coor &c)
{
output<<c.x<<", "<<c.y<<endl;
}
void Coor::Print()
{
cout<<x<<" -- "<<y<<endl;
}
int main()
{
Coor c1(1,2);
Coor c2(2,3);
Coor c3(0,0);
c3 = c1 + c2;
cout<<c3;
return 0;
}