c++学习笔记(一)---数据类型和对象

匿名 (未验证) 提交于 2019-12-03 00:39:02

c++简介:

c++是面向对象的编程,特点可以概括为4个字:封装、抽象、继承、多态,把对象的属性和方法集合成一个独立的系统单位,尽可能隐藏对象的内部细节,对具体的问题进行概括的过程,子类对象拥有与基类相同的全部属性和方法,子类继承基类的属性和行为后,可以具有不同的数据类型和表现行为等特性。

常用代码、函数介绍:

cout:console out(控制台输出)



数据类型:数组、指针、结构:

一个数组可以把很多同类型的数值存在一个变量名下

e.g

#include <iostream>  #define ITEM 10 #define input_add() int main() {     //const unsigned short ITEM = 10;     int num[ITEM];     std::cout << "please input" << ITEM << "int number!\n\n";          input_add();          return 0; }  void input_add() {        for(int i=0; i<ITEM; i++)         {             std::cout << "please input" << i+1 << "number\n";                          std::cin >> num[i]                          while(!(std::cin >> num[i]))                 {                     std::cin.clear();                     std::cin.ignore(100,'\n');                     std::cout << "please input again! \n";                 }                      }          int total = 0;          for(int j=0; j<ITEM; j++)         {             total += num[j]         }          std::cout << "total is " << total << "\n"     std::cout << "mean is " << (float)total/ITEM;     std::cout << "\n" }

int a = 345; char b = 'C'; int *aPointer = &a; char *bPointer = &b;

std::cout << *aPointer; //*aPointer = 123;   会改变a的值

int myArray[3]={1,2,3};

对象的基础--结构:

struct name {     type varName1;     type varName2;     ...... }

e.g

结构和指针:


Student Children = {"xiaoming", "00001", 'M'} Student *pChildren = &Children; (*pChildren).name = "lilei"; (*pChildren).id = "0002";

union mima {     unsigned long birthday;     ....      };


//定义一个汽车的类 #include <iostream> #include <windows.h>  #define FULL_GAS 85  class Car {     public:         std::string color;         std::string engine;         float gas_tank;         unsigned int Wheel;                  void setColor(std::string col);         void setEngine(std::string eng);         void setWheel(unsigned int whe);         void fill_tank(float liter);         void running(void);         void warning(void); }  void Car::fill_Tank(float liter) {     gas_tank +=liter; }  void Car::running(void) {      }  void Car::warning(void) {      }  void Car::setColor(std::string col) {      }  void Car::setEngine(std::string eng) {      }  void Car::setWheel(unsigned int whe) {      }  int main() {     char i;     Car mycar;          mycar.setColor("blue");     mycar.setEngine("v8");     mycar.setWheel(4);          mycar.gas_tank = FULL_GAS;          while(mycar.running())     {         if(mycar.running() < 10 )         {             mycar.warning();             std::cout << "input?(y/n)\n";             std::cin >> i;             if( 'y' == i || 'Y' == i )             {                 mycar.fill_tank(FULL_GAS);             }         }     }     return 0; }


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!