C++ 学习笔记(基础数据类型和语法实例)

#include "iostream"
#include "string"
#include "limits"
#include <cstring>
using namespace std;

static int count = 10;//全局变量,本文件中有效,作用是隐藏

int main()
{
	//count += 1;
	/*              枚举                */
	/*enum  {
	red,//0
	blue = 5,//5
	green,//6
	black//7
	}color_board, color_desk;
	color_board = red;
	*/

	/*              \换行               */
	/*
	cout << "Hello Cod\
er!\r\n";//输出Hello World!     //长字符串换行
	*/
	/*        利用for构造大循环          */
	//for (;;);//大循环区别于while(1)的方式
	/*const 
	//static //在程序的生命周期内保持局部变量的存在,而不需要在每次它进入和离开作用域的时候创建和销毁
	         //应用在修饰全局变量时使变量在声明的文件内受限制,创建仅有一个副本共享
	*/

	/*          基本数据类型           */
	/*
	int i = 1;
	int j = 1;
	int s;
	s = i ^ j;//异或运算

	cout << "s的值为:" << s<<endl;//0
	cout << "type: \t\t" << "--------------size--------------" << endl;
	cout << "bool: \t\t" << "所占字节数:" << sizeof(bool);
	cout << "\t最大值:" << (numeric_limits<bool>::max)();
	cout << "\t\t最小值:" << (numeric_limits<bool>::min)() << endl;
	*/
	/*          基本输入输出流             */
	/*
	char name[50];
	cout << "请输入用户名" << endl;
	cin >> name;
	*/

	/*             结构体变量              */
	
	/*
	struct Books
	{
		char name[50];
		char author[50];
		char date[50];
		int id;
	}book1;

	strcpy_s(book1.name, "C++从入门到精通");
	strcpy_s(book1.author, "Huchangqi");
	strcpy_s(book1.date,"2019.4.19");
	book1.id = 20190419;

	cout << "书名:" << book1.name << endl;
	cout << "作者:" << book1.author << endl;
	cout << "ID号:" << book1.id << endl;
	*/

	/*          指向结构的指针         */
	/*
	struct Books 
	{
		char title[30];
		char author[30];
		char date[30];
		int id;
	}*book,book1;

	strcpy_s(book1.title, "C++从入门到精通");
	strcpy_s(book1.author, "胡长琪");
	strcpy_s(book1.date, "2019.4.19");
	book1.id = 20190419;

	book = &book1;//初始化

	cout << book->title << endl;
	cout << book->author << endl;
	cout << book->date << endl;
	cout << book->id << endl;
	*/

	/*             类             */
	/*
	double volume;
	class Box
	{
		public:
			double length;
			double width;
			double height;
	}Box1,Box2;
	Box1.length = 5.0;
	Box1.width = 6.0;
	Box1.height = 3.0;
	volume = Box1.length * Box1.width * Box1.height;
	cout << "盒子的体积为:" << volume << endl;
	*/

	/*               继承               */
	/*
	class Shape 
	{
		public:
			void setwidth(double w)
			{
				width = w;
			}
			void setlength(double l)
			{
				length = l;
			}
		protected :
				double width;
				double length;
	};
	class Rectange : public Shape
	{
		public:
			double getArea()
			{
				return (width * length);
			}
	}Rect;

	Rect.setwidth(5);
	Rect.setlength(6);

	cout << "对象的面积为:" << Rect.getArea() << endl;
	*/
	/*               多继承               */
	/*
	class Shape//基类
	{
		public:
			void setwidth(double w)
			{
				width = w;
			}
			void setlength(double l)
			{
				length = l;
			}
		protected:
			double width;
			double length;
	};
	class PaintCost//基类
	{
		public:
			double getCost(double area)
			{
				return area * 70;
			}
	};
	class Rectange : public Shape ,public PaintCost
	{
		public:
			double getArea()
			{
				return (width * length);
			}
	}Rect;

	double area;

	Rect.setwidth(5);
	Rect.setlength(6);
	
	area = Rect.getArea();

	cout << "对象的面积:" << Rect.getArea() << endl;
	cout << "花费:" << Rect.getCost(area) << endl;
	*/
	/*			函数重载			*/
	/*
	class printdata 
	{
		public:
			void print(int i)
			{
				cout << "整数为:" << i << endl;
			}
			void print(double f)
			{
				cout << "浮点数为:" << f << endl;
			}
			void print(char c[])
			{
				cout << "字符串为:" << c << endl;
			}
	}pd;
	pd.print(5);
	pd.print(3.0);
	char c[] = "Hello Class!";
	pd.print("Hello Class!");
	pd.print(c);
	*/
	/*			运算符重载			*/
	/*
	class Box
	{
		public:
			double getvalume(void)
			{
				return length * width *height;
			}
			void setLength(double len)
			{
				length = len;
			}

			void setwidth(double wid)
			{
				width = wid;
			}

			void setHeight(double hei)
			{
				height = hei;
			}
			Box operator + (const Box& b)
			{
				Box box;
				box.length = this->length + b.length;
				box.width = this->width + b.width;
				box.height = this->height + b.height;
				return box;
			}
			private:
				double length;
				double width;
				double height;
	}box1,box2,box3;

	double volume = 0.0;

	box1.setLength(6.0);
	box1.setwidth(7.0);
	box1.setHeight(5.0);

	box2.setLength(2.0);
	box2.setwidth(5.0);
	box2.setHeight(3.0);

	volume = box1.getvalume();
	cout << "Volume of Box1 : " << volume << endl;

	volume = box2.getvalume();
	cout << "Volume of Box2 : " << volume << endl;

	box3 = box1 + box2;

	volume = box3.getvalume();
	cout << "Volume of Box3 : " << volume << endl;
	*/
	/*             多态             */
	/*
	class Shape
	{
		protected:
			int width, height;
		public:
			Shape(int a = 0, int b = 0)
			{
				width = a;
				height = b;
			}
			virtual int area()
			{
				cout << "Parent class area:" << endl;
				return 0;
			}
	};
	class Rectangle : public Shape
	{
		public:
			Rectangle(int a = 0, int b = 0) : Shape(a, b)
			{}
			int area()
			{
				cout << "Rectangle class area:" << endl;
				return (width * height);
			}
	};
	class Triangle : public Shape 
	{
		public:
			Triangle(int a = 0, int b = 0) :Shape(a, b)
			{}
			int area()
			{
				cout << "Triangle class area:" << endl;
				return (width * height / 2);
			}
	};

	Shape *shape;
	Rectangle rec(10, 7);
	Triangle tri(10,5);
	
	shape = &rec;
	shape->area();

	shape = &tri;
	shape->area();
	*/
	/*            数据抽象           */
	/*
	class addr
	{
		public:
			addr(int i = 0)
			{
				total = i;
			}
			void addnum(int number)
			{
				total += number;
			}
			int gettotal()
			{
				return total;
			}
			int total;
	}a;
	a.addnum(10);
	a.addnum(20);
	a.addnum(30);
	cout << "total :" << a.gettotal() << endl;
	*/
	/*            C++接口            */
	/*
	class Shape	
	{
		public:
			virtual int getarea() = 0;
			void setwidth(int w)
			{
				width = w;
			}
			void setheight(int h)
			{
				height = h;
			}
		protected:
			int width;
			int height;
	};

	class Rectangle : public Shape
	{
		public:
			int getarea()
			{
				return (width * height);
			}
	}rect;
	class Triangle : public Shape
	{
		public:
			int getarea()
			{
				return (width * height) / 2;
			}
	}tri;

	rect.setwidth(5);
	rect.setheight(7);
	cout << "Total Rectangle area: " << rect.getarea() << endl;
 	
	tri.setwidth(5);
	tri.setheight(7);
	// 输出对象的面积
	cout << "Total Triangle area: " << tri.getarea() << endl;
	*/
	return 0;//终止main()函数,并向调用进程返回值0
}
/*              子函数             */
/*
void function(void)
{
	static int i = 5;//局部静态变量
	i++;
	cout << "变量 i 为:" << i;
	
}
*/

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值