一、入门

国际惯例,输出hello world

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;
int main(){
cout << "hello world" << endl;
return 0;
}

#include<iostream>
using namespace std;

int main(){
char name[10];
cin >> name;
cout << "your name is:"<<name<<endl;
}

由于学过c,所以相同的东西就不过多赘述了

1.for循环练习

1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;

int main(){
for(int i = 1;i <= 100;i++){
if(i % 7 == 0 || i % 10 == 7 || (i / 10 ) % 10 == 7){
cout << i << endl;
}
}
}

2.跳转语句之goto语句

(不怎么使用

作用:无条件跳转语句

语法:goto 标记;

解释:如果标记的名称存在,执行到goto语句时,会跳转到标记的位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

int main(){
cout << "1 xxx" <<endl;
cout << "2 xxx" <<endl;
cout << "3 xxx" <<endl;
goto FLAG;
cout << "4 xxx" <<endl;
cout << "5 xxx" <<endl;
FLAG:
cout << "6 xxx" <<endl;
cout << "7 xxx" <<endl;
return 0;
}

二、类和对象

2.1 封装

将属性和行为作为一个整体,表现生活中的事物并对属性和行为加以权限控制。

语法:class ClassName{ };

实例1:设计一个圆类,求圆的面积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
using namespace std;
#define PI 3.14

class CircleController
{
private:
/* data */
int r;
public:
int calculation(int r){
return PI * r * r;
}
};

int main(){
int r = 10;
CircleController c1;

cout << "圆的面积为:" << c1.calculation(r) << endl;
return 0;
}

实例2:Student类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include<iostream>
#include<string>
using namespace std;

class Student
{
private:
/* data */
string name = "";
int id = 0;
public:
// 获取信息
void setInfot(string name,int id){
this->name = name;
this->id = id;
};
// 输出信息
void printInfor(){
cout << "your name:" << this->name << " and your id:" << this->id << endl;
};
};

int main(){
Student s1;
string name;
int id = 1;
cin >> name;
cin >> id;
cout << "your name:" << name << endl;
cout << "your id:" << id << endl;
s1.setInfot(name,id);
s1.printInfor();
return 0;
}

访问权限有,Class的默认权限是private

1
2
3
公共权限 public
保护权限 protected
私有权限 private

案例3:设计立方体类(Cube)

求出立方体的面积和体积,分别用全局函数和成员函数判断两个立方体是否相等

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<string>
using namespace std;

class Cube
{
private:
/* data */
double m_L;
double m_W;
double m_H;
public:
double getL(){
return m_L;
}

double getW(){
return m_W;
}

double getH(){
return m_H;
}

void measure(Cube c){
if(m_L == c.getL() && m_W == c.getW() && m_H == c.getH()){
cout << "yes" << endl;
}
else{
cout << "no" << endl;
}
}

void setData(double L,double W,double H){
m_L = L;
m_W = W;
m_H = H;
}

double getVolume(){
return m_L * m_W * m_H;
}

double getArea(){
return 2 * (m_L * m_W + m_W * m_H + m_L * m_H);
}
};


void Measure(Cube c1,Cube c2){
if(c1.getL() == c2.getL() && c1.getW() == c2.getW() && c1.getH() == c2.getH()){
cout << "yes" << endl;
}
else{
cout << "no" << endl;
}
}

int main(){
Cube c1;
Cube c2;
c1.setData(3,4,5);
c2.setData(1,2,3);
c1.measure(c2);
Measure(c1,c2);

cout << "your volume:" << c1.getVolume() << endl;
cout << "your area:" << c1.getArea() << endl;
}

案例4:点和圆之间的关系

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<math.h>
using namespace std;

class Point
{
private:
int x,y;
public:
void setPoint(int x,int y){
this->x = x;
this->y = y;
}

int getX(){
return x;
}

int getY(){
return y;
}
};

class Circle
{
private:
/* data */
int r;
Point p;
public:
void setR(int r){
this->r = r;
}
void setCenter(int x,int y){
p.setPoint(x,y);
}

int getR(){
return r;
}

int getCenterX(){
return p.getX();
}

int getCenterY(){
return p.getY();
}
};

void measure(Circle c,Point p){
int res;
res = sqrt((c.getCenterX() - p.getX()) * (c.getCenterX() - p.getX()) + (c.getCenterY() - p.getY()) * (c.getCenterY() - p.getY()));
if(res == c.getR()){
cout << "the point on the circle" << endl;
}
else if(res > c.getR()){
cout << "the point out the circle" << endl;
}
else{
cout << "the point in the circle" << endl;
}
}

int main(){
Circle c;
c.setR(10);
c.setCenter(0,0);
Point p;
p.setPoint(3,4);
measure(c,p);
}

2.2 对象的初始化和清理

构造函数和析构函数

  • 构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用;

  • 析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作。

实例1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<iostream>
#include<string>
using namespace std;

class Person
{
private:
/* data */
string name;
int age;
public:
Person();
~Person();
void setName(string name){
this->name = name;
}
string getName(){
return this->name;
}
void setAge(int age){
this->age = age;
}
int getAge(){
return this->age;
}
void print(){
cout << "your name:" << name << " your age:" << age << endl;
}
};

Person::Person()
{
cout << "contruct function" << endl;
}

Person::~Person()
{
cout << "BYE!" << endl;
}

int main(){
Person p1;
p1.setName("Ameuu");
p1.print();

}

构造函数的分类及调用

分类:

​ 按参数分类:有参、无参

​ 按类型分类:普通、拷贝

调用:

​ 括号法、显示法、隐式转换法

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<string>
using namespace std;
/**
* 1.构造函数的分类及调用
* 分类:有参 无参/ 普通 拷贝
*
* */
class Person
{
public:
/* data */
string name;
int age;
public:
Person(){
cout << "contruct funtion" << endl;
}
~Person(){
cout << "destruct function" << endl;
}

// 有参构造函数
Person(int age){
this->age = age;
cout << "the contrust funtion include args" << endl;
}

// 拷贝构造函数
Person(const Person &p){
//将传入的类的所有属性拷贝到本类身上
age = p.age;
cout << "copy contruct function" << endl;
}
};

int main(){

// 括号法
// Person p1;
// Person p2(10);
// Person p3(p2);
// cout << "*****************************" << endl;

// 显示
// Person p1;
// Person p2 = Person(10);
// Person p3 = Person(p2);

// Person(10); // 匿名对象 当前行执行结束之后,系统立即回收
// // 注意:不能利用拷贝构造函数初始化匿名对象
// cout << "*****************************" << endl;

// 隐式转换
Person p1 = 10;
Person p2 = p1;
return 0;
}

拷贝构造函数的调用

​ 1.使用一个创建完毕的对象来初始化一个新对象

​ 2.值传递的方式给函数参数传值

​ 3.值方式返回局部对象

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;

class Person{
private:
int age;
public:
Person(){
cout << "contruct funtion" << endl;
}
~Person(){
cout << "destruct function" << endl;
}

Person(const Person &p){
cout << "copy contruct funtion" << endl;
}
};

void test(Person p){

}

Person test1(){
Person p;
return p;
}

void test2(){
Person p = test1();
}

int main(){
// 1.使用一个创建完毕的对象来初始化一个新对象
Person p1;
Person p2 = test1();

// 2.值传递的方式给函数参数传值
// test(p1);

// 3.值方式返回局部对象
// Person p3 = test1();
// test2();
}