面向对象(oop):c++ Java PHP Python
面向对象(oop)语言的四(三)大特征是什么?
- 抽象-------如果问三大特征,省略这个
- 封装/隐藏:在语言上,是通过访问限定符体现的(public公共变量,哪里都可以访问 protected受保护的,类内和子类可直接访问 private私有的,只有类内可直接访问)
- 继承
- 多态
面向对象:
oop中类和类之间常用的关系是什么
- 组合 a part of... 一部分
- 继承 a kind of... 一种
struct和class都可以定义类,区别是什么
- struct定义的类是 public
- class定义的类是 private
如何查看对象的内存布局
打开vs的命令行工具。
eg:
class CGoods size(32):
+---
0 | _name
20 | _amount
24 | _price
+---
- 结论:一个类型定义的很多对象,私有一份成员变量,但是共享成员方法,只有成员变量才占用对象的内存,成员方法是函数,编译后生成的是指令,存储在.text代码段,是所有对象共享的!!!!
解释一下this指针的作用
- 同一个类型定义的很多对象,是共享它们的成员方法的,那么一个成员方法,怎么区分不同对象的数据呢? 编译后,成员方法都会多出来一个形参变量,就是this指针,指向调用该方法的对象,this指针指向哪个,方法处理的就是相应的对象的成员
new和malloc的区别?delete和free的区别?
- malloc和free都是C语言的库函数, new和delete一般叫做运算符(如:sizeof)
- malloc是按字节分配内存的。new是按指定类型的个数分配内存的 eg:new int[20] 直接分配了20个元素的int类型的数组
- malloc开辟的内存不带初始化操作。new开辟内存可以自带初始化 eg:int *p=new int(100); p[0]=100
- free是需要传入释放内存的起始地址就可以。delete是需要区分释放的是单个元素内存还是数组内存,在释放数组内存时,delete和指针之间,要加上[]。eg:delete []p
- malloc开辟内存失败会抛出bad_alloc类型的异常,用try和catch捕捉
new int[20]和new int[20]()有什么区别
new int[20] 开辟的值都是随机值
new int[20]() 开辟的值都是0
类的成员方法在类体内定义和在类外定义有什么区别?
- 类外定义的成员方法,在方法名字前面加类类型的作用域
- 类外定义的方法在调用时,有函数的正常开销。而类体内定义的函数,都被自动处理成inline内联函数
构造函数和析构函数
- 构造函数:给对象做初始化
- 析构函数:给对象释放占用的其他资源
都没有返回值,函数名也和类名一样,析构函数前面加个~号
- 构造函数可以添加任意的形参变量,所以构造函数可以重载
- 析构函数不可以带参数,不能重载
注:调用对象默认构造函数的时候,不能在对象后面添加空的(),否则成函数声明了
对象的浅拷贝和深拷贝问题!
- 如果对象发生浅拷贝问题,就必须得提供自定义的拷贝构造函数和operator= 。
- 发生浅拷贝的主要原因是两个对象指向了同一块内存。(属性有指针这种)
基本代码
class CGoods{
public://(行为)
//给商品对象注册具体的商品信息的
void registerGoods(char *n, int a, double p)
{
strcpy(this->_name, n);
this->_amount = a;
this->_price = p;
}
//setxxx函数,专门供外部设置成员变量的值
void setName(char *n){ strcpy(this->_name, n); }
void setAmount(int a){ this->_amount = a; }
void setPrice(double p){ this->_price = p; }
//getxxx函数,专门供外部获取私有的成员变量的值
char* getName(){ return this->_name; }
int getAmount(){ return this->_amount; }
double getPrice(){ return this->_price; }
//打印商品的详细信息的
void show();
private:
char _name[20];
int _amount;
double _price;
};
//打印商品的详细信息的
void CGoods::show()
{
cout << "name:" << this->_name << endl;
cout << "amount:" << this->_amount << endl;
cout << "price:" << this->_price << endl;
}
int main(int argc, char* argv[])
{
CGoods g1;
// CGoods::registerGoods(&g1, "面包", 20, 10.0);
g1.registerGoods("面包", 20, 10.0);
// CGoods::show(&g1)
g1.show();
cout << sizeof(g1) << endl;
CGoods g2;
// CGoods::setName(&g2, "香肠")
g2.setName("香肠");
g2.setAmount(30);
g2.setPrice(5.0);
g2.show();
cout << sizeof(g2) << endl;
return 0;
}
/*
队列
*/
class CQueue{
public:
CQueue();
CQueue(int size);
CQueue(const CQueue &src)//拷贝
{
_Pque = new int[src._size];
memcpy(_Pque, src._Pque, sizeof(int)*src._size);
_front = src._front;
_rear = src._rear;
_size = src._size;
}
void operator= (const CQueue &src)
{
if (this == &src)
{
return;
}
delete[]_Pque;
_Pque = new int[src._size];
memcpy(_Pque, src._Pque, sizeof(int)*src._size);
_front = src._front;
_rear = src._rear;
_size = src._size;
}
~CQueue();
void push(int val);
void pop();
int front();
bool empty();
bool full();
private:
void Resize();
int *_Pque;
int _rear;
int _front;
int _size;
};
CQueue::CQueue()
{
_rear = _front = 0;
_Pque = new int[SIZE];
try{
_Pque = new int[SIZE];
}
catch (bad_alloc &e)
{}
_size = SIZE;
}
CQueue::CQueue(int size)
{
_rear = _front = 0;
_Pque = new int[size];
_size = size;
}
CQueue::~CQueue()
{
delete[]_Pque;
_Pque = NULL;
}
void CQueue::push(int val)
{
if (full())
Resize();
_Pque[_rear] = val;
_rear = (_rear + 1) % _size;
}
void CQueue::pop()
{
if (empty())
return;
_front = (_front + 1) % _size;
}
int CQueue::front()
{
return _Pque[_front];
}
bool CQueue::empty()
{
return _rear == _front;
}
bool CQueue::full()
{
return (_rear + 1) % _size == _front;
}
void CQueue::Resize()
{
int *ptmp=new int[_size * 2];
for (int i = _front, j = 0; i != _rear; j++, i = (i + 1) % _size)
{
ptmp[j] = _Pque[i];
}
_front = 0;
_rear = _size - 1;
_size *= 2;
delete[]_Pque;
_Pque = ptmp;
}
/*
栈
*/
class CStack{
public:
CStack();
CStack(int size);
CStack(const CStack &src)//拷贝
{
_pstack = new int[src._size];
memcpy(_pstack, src._pstack, sizeof(int)*src._size);
_top = src._top;
_size = src._size;
}
void operator=(const CStack &src)//赋值
{
if (this == &src)
{
return;
}
delete[]_pstack;
_pstack = new int[src._size];
memcpy(_pstack, src._pstack, sizeof(int)*src._size);
_top = src._top;
_size = src._size;
}
~CStack();
void push(int val);
void pop();
int top();
bool empty();
bool full();
private:
int *_pstack;
int _top;
int _size;
void reSize();
};
CStack::CStack()
{
_pstack = new int[SIZE];
_top = 0;
_size = SIZE;
}
CStack::CStack(int size)
{
_pstack = new int[size];
_top = 0;
_size = size;
}
CStack::~CStack()
{
delete[]_pstack;
_pstack = NULL;
}
void CStack::push(int val)
{
if (full())
reSize();
_pstack[_top++]=val;
}
void CStack::pop()
{
if (empty())
return;
_top--;
}
int CStack::top()
{
return _pstack[_top - 1];
}
bool CStack::empty()
{
return _top == 0;
}
bool CStack::full()
{
return _top == _size;
}
void CStack::reSize()
{
int *ptmp = new int[_size * 2];
memcpy(ptmp, _pstack, sizeof(int)*_size);
_size *= 2;
delete[]_pstack;
_pstack = ptmp;
}
int main()
{
CQueue que(5);//初始值为5
// 以系统时间作为种子,种随机数
srand(time(NULL));
// 循环20次,随机[0, 100]之间的整数,入队列
for (int i = 0; i < 20; ++i){
que.push(rand() % 100);
}
// while循环判断,队列不为空,则获取对头元素,打印,并出队,一直循环该操作,
// 直到打印完所有的队列元素
while (!que.empty())
{
int front = que.front();
cout << front << " ";
que.pop();
}
return 0;
}