1、父类的指针可以指向子类对象地址
2、想让子类能够重写父类函数且能够调用子类自己的同名函数,就要把函数命名为virtal虚函数,
#include <iostream>
#include <VECTOR>
using namespace std;
class Shape//形状
{
public:
<span style="background-color: rgb(255, 255, 102);"> virtual </span>void DrawSelf()//绘制自己
{
cout << "输出shape" << endl;
}
};
class Polygo:public Shape//多边形
{
public:
// void DrawSelf() //绘制自己
// {
//<span style="white-space:pre"> </span>cout << "输出polygo" << endl;
// }
};
void OutputShape1( <span style="background-color: rgb(255, 255, 0);">Shape &arg</span>) //<span style="background-color: rgb(255, 255, 0);">当传入子类的对象时,会发生向上转型,但是由于是引用类型(指针类型也可以),向上转型时子类信息不会丢失,</span>
//<span style="background-color: rgb(255, 255, 0);">当父类中的调用函数是virtual虚函数时,子类也有该函数时调用子类的,子类没有就调用父类的</span>
{
arg.DrawSelf(); // 根据具体vehicle的类型调用对应的run()
}
void main()
{
Shape sh;
Polygo p;
OutputShape1(<span style="background-color: rgb(255, 255, 0);">p)</span>//输出polygo输出circ
}
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">3、访问类的私有成员①</span><span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace; font-size: 14px; line-height: 24px; white-space: pre-wrap;">类的私有成员只能在类内访问。。全局函数是无法访问的。</span><span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace; font-size: 14px; line-height: 24px; white-space: pre-wrap;">但是可以定义这样的接口,如 int getvalue(){return i};i为类的私有成员</span><span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace; font-size: 14px; line-height: 24px; white-space: pre-wrap;">。外界通过对象调用该函数来达到访问私有成员的目的</span>
还有一种办法访问类的私有成员:
当我们的工程总类越来越多的时候,我们很难避免类与类之间相互操作。倘若每个类之间相互操作都通过函数进行,那可想我们编写代码是多么的繁琐,想投机取巧走小路么?是的,C++提供了这种捷径,让我们直接对一个类的私有成员进行操作——当然,我极力反对这种做法,因为它打破了类的封装性。
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">3、访问类的私有成员</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"> ①</span><span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace; font-size: 14px; line-height: 24px; white-space: pre-wrap;">类的私有成员只能在类内访问。。全局函数是无法访问的。</span><span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace; font-size: 14px; line-height: 24px; white-space: pre-wrap;">但是可以定义这样的接口,如 int getvalue(){return i};i为类的私有成员</span><span style="color: rgb(51, 51, 51); font-family: arial, 'courier new', courier, 宋体, monospace; font-size: 14px; line-height: 24px; white-space: pre-wrap;">。外界通过对象调用该函数来达到访问私有成员的目的</span>
②当我们的工程总类越来越多的时候,我们很难避免类与类之间相互操作。倘若每个类之间相互操作都通过函数进行,那可想我们编写代码是多么的繁琐,想投机取巧走小路么?是的,C++提供了这种捷径,让我们直接对一个类的私有成员进行操作——当然,我极力反对这种做法,因为它打破了类的封装性。
#include <iostream>
using namespace std;
class A
{
public:
int _a;
//A(){ _a = 1; }
void print()
{
printf("%d\n", _a);
}
};
class B: public A
{
public:
int _a;
B()
{
_a = 2;
}
B(int a)
{
_a=a;
}
};
int main()
{
B b(7);
printf("%d\n", b._a); //<span style="background-color: rgb(255, 255, 0);">子类覆盖父类的变量</span>
b.print(); //<span style="background-color: rgb(255, 255, 0);">B类对象调用了父类的函数,同名的变量使用父类的</span>
return 0;
}
#include <iostream>
using namespace std;
class B
{
public:
int data;
int a;
public:
B()
{
cout<<"default constructor"<<endl;
}
~B()
{
cout<<"destructed"<<endl;
}
B(int i):data(i)
{
cout<<"constructor by parameter"<<data<<endl;
}
};
B Play(B b)
{
return b;
}
int main()
{
B temp = Play(5);
cout<<temp.data<<endl;
return 0;
}
1 2 3 |
| constructor by parameter5 destructed 5 destructed |
Play(5)通过隐含类型转换调用B:B(int i),返回时调用析构函数,temp调用拷贝构造函数。
#include <iostream>
using namespace std;
class CA
{
public:
void f()
{
cout << "CA f()" << endl;
}
virtual void ff()
{
cout << "CA ff()" << endl;
f();
}
};
class CB : public CA
{
public :
virtual void f()
{
cout << "CB f()" << endl;
}
void ff()
{
cout << "CB ff()" << endl;
f();
CA::ff();
}
};
class CC : public CB
{
public:
virtual void f()
{
cout << "C f()" << endl;
}
};
int main()
{
CB b;
CA *ap = &b;
CC c;
CB &br = c;
CB *bp = &c;
ap->f();
cout << endl;
b.f();
cout << endl;
br.f();
cout << endl;
bp->f();
cout << endl;
ap->ff();
cout << endl;
bp->ff();
cout << endl;
return 0;
}
输出:
01
02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 |
| CA f() CB f() C f() C f() CB ff() CB f() CA ff() CA f() CB ff() C f() CA ff() CA f() |