c++封装(上)

类和对象:

访问限定符:public 、protected、private;

对象实例化:从栈实例化

class tv{

public:

char name[20];

int type;

void changeVol();

void power();

};

int main(void){

TV tv;

TV tv[20];

return 0;

}

从堆实例化:

class tv{

public:

char name[20];

int type;

void changeVol();

void power();

};

int main(){

TC *p=new TV();

TV *q=new TV[20];

delete p;

delete []q;

return 0;

}

对象成员的访问:

TV tv;

tv.type=0;

tv.changeVol();

return0;

堆:TV*p=new TV();

p->type=0;

p->changVol();

delete p;

p=NULL;

return;

///实例化一个类//

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class Coordinate{
public:
int x;
int y;
void printX()
{
cout << x << endl;
}
void printY()
{
cout << y << endl;
}
};
int main(){


Coordinate *p = new Coordinate();
if (p == NULL){
return0;
}
p->x = 100;
p->y = 20;


p->printX();
p->printY();
delete p;
p = NULL;
system("pause");
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值