(三) C++快速入门(简单版)

基础语句

  • #include

#include 是一个预处理指令,用来在程序中包含一个头文件,这个头文件中有定义了 cin 和 cout 等输入输出流对象的声明和定义,使用这个指令可以方便地在程序中使用这些对象,而不用手动编写它们的声明和定义。

  • using namespace std;

using namespace std 是一个编译指令,用来指示编译器在程序中使用 std 命名空间中的标识符,从而不用再每次使用 cout,cin 等标准输入输出流的时候都加上 std:: 的前缀。这样可以让代码更加简洁易读,但是这种使用方式有时也会引发命名冲突和代码混淆等问题,因此需要谨慎使用。

输出

#include <iostream>
using namespace std;

int main(){
cout <<"Hello world~" << "nihao";
// 这里是连着的输出
count <<"I am learning!" << endl; //回车换行标识符
}

输入

#include <iostream>
using namespace std;

int main(){
	int x;
	cin >>x;
	cout <<"This is x: "<< x<<endl;
	return 0;
}

字符串

  • 字符串需要引入一个新的库
#include <string>

string greet="hello";

数组

#include <iosteam>
#include <string>

int main(){
string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
int num[3] = {10, 20, 30};
int num[] = {10, 20, 30}; //也可以不用赋值
}

指针和引用

  1. 引用的概念
  • 引用就是某一变量(目标)的一个别名, 也就是说, 它是某个已存在变量的另一个名字. 对引用的操作与对变量名的操作完全一样
  1. 引用的初始化
    类型标识符 &引用名=目标变量名
  • 上述格式中, &并不是取地址操作符, 而是起标识作用, 引用声明完成以后相当于目标变量有两个名称. 例:
    int a = 10;
    int &b = a;
    
  1. 引用注意事项
  • 引用在定义时必须初始化, 如int &b;是错误的.
  • 引用在初始化时不能绑定常量.
  • 引用一旦初始化, 其值就不能再更改.
    int a = 10;
    int b = 20;
    int &c= a;
    c = b;
    cout<<a;
    // a = 20
    
  1. 不能建立引用的数组, 如int &ref[3] = {32,3};, 但是可以建立数组的引用, 如int arr[3]; int (&tef)[3] = arr;

  2. 引用应用

  • C++增加引用的类型, 主要的应用就是把它作为应用的传递
#include <iostream>
using namespace std;

void swap(int &x, int &y){
	int temp = x;
	x = y;
	y = temp;
}

int main(){
	int a, b;
	cout<<"intput two nums: ";
	cin >>a>>b;
	swap(a,b);
	cout<<"swap: "<<a<<" "<<b<<endl;
	return 0;
}
引用和指针的区别
  1. 不存在空引用, 引用必须链接到一块合法的内存.
  2. 一旦引用被初始化一个对象, 就不能被指向到另一个对象. 指针可以在任何时候指向到另一个对象.
  3. 引用比如在创建时被初始化, 指针可以在任何时间被初始化

指针

  • 定义时, 需要用* 表示指针指向的类型, 如int *p;, int* p;
  • 使用时, 指针不带*表示的是地址, 带*表示的是地址表示的值.
    int main(){
    	string food = "Pizza";
    	string *ptr = &food; //此时ptr里面包含的是地址,带*表示的是地址的值
    
    	*ptr = "Hamburger"; //此时修改ptr地址的值变量food的值也会被修改, 因为它们的地址是一致的.
    }
    

函数

 //函数如果在main之后定义,需要在前面加声明
void myFunction();

int main(){
	myFunction();
	return 0;
}

void myFunction(){
	cout <<"I just got my executed!";
}

类与对象

  • 类是对象的模板, 对象是类的一个实例. 当单个对象被创建时, 它们继承了类中的所有变量和功能.

    class Car{
    	public:
    		string brand;
    		string model;
    		int year;
    }; //注意这里有分号
    
    int main(){
    	Car carObj1; //这里进行了实例化
    	carObj1.brand = "BMW";
    	return 0;
    
    }
    
类里面的方法
#include <string>
using namespace std;

class Car{
	public:
		string brand;
		string model;
		int year;
	void second(){
		cout<<"hello"<<endl;};
	int mythod(int i);
	};

//重定义
int Car::mythod(int i){
	cout<< "this is : "<<i;
	return 0;
	}

int main(){
	Car carObj1;
	carObj1.brand = "BMW";
	carObj1.second();
	carObj1.mythod(4);
	return 0;
}
类的构造函数
  • 名称与类名相同

    class Car{
    	public:
    		string brand:
    		string model;
    		int year;
    		Car(string x, string y, int z); //构造函数
    }; //注意分号
    
    Car::Car(string x, string y, int z){
    	brand = x;
    	model = y;
    	year = z;
    }
    
    int main(){
    	Car carObj1("BMW", "XS", 1999);
    }
    
私有和公有属性
class  myclasss{
	int b; //默认属性是私有属性;
public:
	int x;
	int y;
private:
	int a; //与b一样
};
对私有属性的访问和修改
class Employee{
private:
	int salary;
public:
	// set
	void SetSalary(int s){
		salary=s;
	}
	
	// get
	int GetSalary(){
		return salary;
	}
	
};

int main(){
	Employee myobj1;
	myobj1.SetSalary(500);
	myobj1.GetSalary();
	return 0;
}
类的继承
class Vehicle{
	public:
		string brand="Ford";
		void honk(){
		cout <<"didi";
		}
};

// 下面是继承
class Car: public Vehicle{
	public:
		string model="Mustang";
}

//当然也可以继承好几个父类
class childclass: public father1, public father2{

};
只有子类可以调用的属性
class Employee{
	protected: //注意这里如果是private, 则只有父类自己可以访问, 子类也是无法访问修改的.
		int salary;
};

class Programmer: public Employee{
	public: 
		int bonus;
		void setSalary(int s){
			salary=s;
		}
		int getSalary(){
			return salary;
		}
}
posted @ 2023-06-15 09:43  sdulyq  阅读(61)  评论(0)    收藏  举报