天天看点

7.8课堂笔记

1.有关指针

一个*是一级指针

两个*是二级指针

int main()
{
	int a = 10;
	int b = 20;
	int *p = &a;

	int** s = NULL;
	s = &p;
	*s = &a;
	**s = 100;
	*s = &b;
	**s = 200;
}
           

&s==>0x

S==>&p;

*s==>p==>&a;

**s==>*p==>a;

2.C++中&的用法

1>位运算符

2>逻辑运算符

3>引用

int main()
{
	char a = 10, b = 20;
	char c = a & b;
	char *cp = &a;
	char& x = a;
	return 0;

}
           

Cp=&x;

Cp=&a;

4>***此符号不同于指针存在二级现象

Char &z=a;

Char &&w=z;

3.对象的属性、类型、方法

1>属性值决定了对象所处的状态(定义为私有)

例:

人在不同年龄阶段的状态是不同的,而外界是不能轻易改变的,所以为私有

a为变量

c为对象

int main()
{
	int a = 10;
	CGoods c1;
	c1.GetAmount = 100;
	c1.GetPrice = 10;
	return 0;
}
           

2>在类内声名,类外定义==再类内声名,定义

#include<stdio.h>
#include<stdlib.h>//rand
#include<assert.h>
#include<string.h>

class CGoods//设计类型
{
private:
	char Name[20];
	int Amount;
	float Price;
	float Total_vaule;
public:

	//void RegisterGoods(CGoods *this,count char*, int, float);
	void RegisterGoods(const char*, int, float);//输入数据
	//void CountTotal(CGoods *this)
	void CountTotal()//计算商品总价值
	{
		Total_vaule = Amount * Price;//类内声名
	}
	void GetName(char name[])//读取商品名
	{
		strcpy_s(name, 20, Name);
	}
	//int GetAmount(CGoods *this)
	int GetAmount(void)//读取商品数量
	{
		return Amount;
	}
	//float GetPrice(CGoods *this)
	float GetPrice(void)//读取商品单价
	{
		return Price;
	}
	//float GetTotal_vaule(CGoods *this)
	float GetTotal_vaule(void)//读取商品总价格
	{
		return Total_vaule;
	}
};
//void CGoods::RegisterCoods(CGoods *this,const char*name, int amount, float price);
void CGoods::RegisterGoods(const char*name, int amount, float price)//说明RegisterGoods是CGoods的成员方法,只不过RegisterCoods在类外定义
{
	strcpy_s(this->Name, 20, name);
	this->Amount = amount;
	this->Price = price;
	Total_vaule = amount * price;
}
int main()
{
	CGoods c1, c2;
	c1.RegisterGoods("iphone", 10, 6800);
	//c1.RegisterCoods(&c1,"iphone", 10, 6800);
	c1.CountTotal();
	c2.RegisterGoods("huawei", 12, 7800);
	//c2.RegisterCoods(&c2,"huawei", 12, 7800);
	c2.CountTotal();
 }
           

4.This 指针

编译器针对程序员自己设计的类型分三次编译

第一:识别和记录类体中属性的名称,类型和访问限定,与属性在类体中的位置无关。

如:class CGoods中的Name,Aount,Price,Totai_vaule;

第二:识别和记录类体中的函数原型(返回类型+函数名+参数列表),形参的默认值,访问限定。不能识别函数体。

第三:改写在类中定义函数的参数列表和函数体,改写对象调用成员函数的形式;

全局函数不会有this指针

class CGoods
{
public:
	char Name[20];
	int Amount;
	float Price;
	float Total_vaule;
};
CGoods c1;
CGoods* this;
this = &c1;
c1.Price = 10;
           

This=>&c1;

*this=>c1;

(*this).Price=10;//.的优先级高于*的优先级,所以要加括号

This->Price=10;

5.有关栈的了解

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<string.h>
#include<iostream>
using namespace std;

template<class Type>
class SeqStack//栈 先进后出
{
	Type* data;
	int maxsize;
	int top;
public:
	SeqStack(int sz = 10):maxsize(sz),top(-1)
	{
		data = (Type*)mallocz(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 ==0; }
	bool Is_Full()const { return GetSize() == maxsize; }
	bool Push(const Type& x)
	{
		if (Is_Full())return false;
		data[++top] = x;
		return ture;
	}
	Type& GetTop()
	{
		return data[top];
	}
	const Type &GetTop const
	{
		return data[top];
	}
	void Pop()
	{
		--top;
	}
	void Clear()
	{
		top = -1;
	}
};
int main()
{
	SeqStack<int>ist = SeqStack<int>();
	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;
}