实践周(四)

char& x=a &表示:引用
缺省函数:

拷贝构造函数、构造函数、析构函数,赋值运算符、取址运算符、移动构造、移动复制、赋值语句

构造函数

给创建的对象建立一个标识符;
为对象数据成员开辟内存空间;
完成对象数据成员的初始化。

指针的应用
在这里插入图片描述
#include
using namespace std;
class CGoods {
private:
char Name[20];
int Amount;
float Price;
float Total_value;
public:
//void RegisterGoods(CGoods * this, const char*, int, float);//输入数据
void RegisterGoods(const char*, int, float);//输入数据
//void CountTotal(CGoods * this)
void CountTotal() {
Total_value = Amount * Price;
}
//void GetName(CGoods * this, char name[])
void GetName(char name[]) { //读取商品名
strcpy_s(name, 20, Name);
}
//int GetAmount(CGoods *this) //读取商品数量
float GetAmount() {
return Amount;
}
//float GetPrint(CGoods this) //读取商品单价
float GetPrice() {
return Price;
}
//float GetTotal_value(CGoods this) //读取商品总价值
float GetTotal_value() {
return Total_value;
}
};
// void CGoods::RegisterGoods(CGoods * this, const char
name, int amount.float price)
void CGoods::RegisterGoods(const char
name, int amount, float price) {
strcpy_s(this->Name, 20, name);
this->Amount = amount;
this->Price = price;
}
int main01() {
CGoods c1, c2;
c1.RegisterGoods(“iphone”, 10, 6800);
//RegisterGoods(&c1,“iphone”, 10, 6800);
c1.CountTotal();
c2.RegisterGoods(“huawei”, 12, 7800);
//RegisterGoods(&c2,“huawei”, 12, 7800);
c2.CountTotal();
return 0;
}
类模板
如果要对int、double类型的两个数进行交换我们要写两个函数,但用函数模板时只需要写一个函数即可。模板定义如下:

template
或者

template
其中,template是声明一个模板,typename是声明一个虚类型T,这个T可以代替int、double等基本数据类型,那为什么还有class?因为最初是用class声明一个T来表示通用数据类型,但考虑到class会和“类”混淆,就改用typename进行定义,同时保留了class,这两者效果相同
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include
using namespace std;
template //<>里面是类型
class SeqStack { //栈 先进后出
private:
Type* data; //若上列<>里面为int类型,则data就代表int,供下列代码使用。
int maxsize;
int top;
public:
SeqStack(int sz = 10) :maxsize(sz), top(-1) {
data = (Type*)malloc(sizeof(Type) * maxsize);
if (data == NULL)exit(1);
}
~SeqStack() {
free(data);
data = NULL;
maxsize = 0;
top = -1;
}
int GetSize()const { return top + 1; }
bool Is_Empty()const { return top == -1; }
bool Is_Full() const { return GetSize() == maxsize; }
bool Push(const Type& x) {
if (Is_Full())return false;
data[++top] = x;
return true;
}
Type& GetTop() {
return data[top];
}
const Type& GetTop()const {
return data[top];
}
void Pop() {
–top;
}
};
int main() {
int i = 0;//int i(0); int i=int(0);
SeqStack ist = SeqStack();
ist.Push(12);
ist.Push(23);
ist.Push(34);
ist.Push(45);
while (!ist.Is_Empty()) {
int x = ist.GetTop();
ist.Pop();
cout << x << endl;
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值