C++快速入门(小牛教编程)
C++编程入门教程
欢迎来到C++编程入门教程!在本课程中,我们将学习如何使用C++编程语言创建基本的计算机程序。
Slide 1: 什么是C++?
C++是一种通用编程语言,用于创建各种各样的应用程序。
C++语言是C语言的扩展,它在C语言的基础上增加了一些新的特性,例如面向对象编程和异常处理。
Slide 2: 如何创建一个C++程序?
C++程序是由多个代码文件组成的,其中一个文件包含主函数,这个文件通常被称为“main”。
使用一个文本编辑器(例如记事本)来编写C++代码。将代码保存为一个.CPP文件。
使用编译器(例如gcc或Visual Studio)将C++代码编译成可执行文件。可执行文件通常具有扩展名.EXE或.OUT。
运行可执行文件即可运行程序。
Slide 3: Hello World!
在C++中,同学们可以使用cout语句输出文本到控制台。
下面是一个示例程序,它将输出“Hello, World!”到控制台。
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}Slide 4: 变量和数据类型
在C++中,同学们可以使用变量来存储数据。变量必须先声明,然后才能使用。
C++有不同类型的数据,例如整数,浮点数,字符等。同学们必须为每个变量指定数据类型。
int age = 12; float price = 2.99; char letter = 'A';
Slide 5: 运算符
C++中有许多运算符,用于执行不同类型的操作。
运算符是用于执行特定操作的符号或关键字。
C++中有多种运算符,包括算术运算符、关系运算符、逻辑运算符和赋值运算符。
运算符的操作数可以是变量、常量或表达式。
例如,同学们可以使用+运算符来将两个数字相加。
下面是一些常用的C++运算符:
+ 加 - 减 * 乘 / 除 % 取余
int a = 10; int b = 5; int c = a + b; // 算术运算符 bool d = a > b; // 关系运算符 bool e = (a > b) && (a < 20); // 逻辑运算符 a += b; // 赋值运算符
Slide 6: 控制流
控制流是根据条件或循环控制代码的执行顺序。
在C++中,同学们可以使用if语句和switch语句来控制程序的执行。
同学们还可以使用for循环、while循环和do-while循环来重复执行代码。
int score = 90;
if (score >= 60) {
cout << "You passed the exam!" << endl;
} else {
cout << "You failed the exam." << endl;
}
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
default:
cout << "Other day" << endl;
break;
}
for (int i = 1; i <= 10; i++) {
cout << i << endl;
}
int j = 1;
while (j <= 10) {
cout << j << endl;
j++;
}
int k = 1;
do {
cout << k << endl;
k++;
} while (k <= 10);Slide 7: 数组
数组是一组具有相同数据类型的变量的集合。
在C++中,同学们可以使用数组来存储和处理大量的数据。
要声明一个数组,同学们需要指定数据类型和数组的大小。
int numbers[5] = {1, 2, 3, 4, 5};Slide 8: 函数
函数是一段可重复使用的代码块,用于执行特定的任务。
在C++中,同学们可以创建自己的函数,以便在程序中多次使用。
要创建一个函数,同学们需要指定函数的名称、参数列表和返回类型。
int addNumbers(int a, int b) {
int sum = a + b;
return sum;
}
int result = addNumbers(10, 20);
cout << result << endl;Slide 9: 结构体(Structures)
结构体是一种自定义的数据类型,它允许我们将不同类型的数据组合在一起,并用一个名称来表示这个数据组。结构体通常用于表示复杂的数据对象,例如学生信息、公司员工信息等。
下面是一个示例:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
float score;
};
int main() {
Student s1;
s1.name = "Alice";
s1.age = 20;
s1.score = 90.5;
cout << "Name: " << s1.name << endl;
cout << "Age: " << s1.age << endl;
cout << "Score: " << s1.score << endl;
return 0;
}Slide 10:指针(Pointers)
指针是一种用来存储内存地址的变量。它们通常用于动态分配内存、函数传递和数组操作等场景中。指针也可以用于访问和修改内存中的数据。
下面是一个示例:
#include <iostream>
using namespace std;
int main() {
int a = 5;
int *p = &a; // 定义一个指向a的指针
cout << "Value of a: " << a << endl;
cout << "Address of a: " << &a << endl;
cout << "Value of p: " << p << endl; // 输出p的值,即a的地址
cout << "Value pointed by p: " << *p << endl; // 输出p所指向的值,即a的值
*p = 10; // 修改p所指向的值
cout << "New value of a: " << a << endl;
return 0;
}Slide 11: 类和对象
在C++中,同学们可以使用面向对象编程来创建类和对象。
类是一种数据类型,用于表示具有相似属性和行为的对象的集合。
对象是类的实例,它具有类定义的属性和行为。
class Dog {
public:
string name;
int age;
void bark() {
cout << "Woof!" << endl;
}
};
Dog myDog;
myDog.name = "Buddy";
myDog.age = 3;
myDog.bark();练习题:
1、编写一个C++程序,将两个数字相加并输出结果。
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 3;
int sum = a + b;
cout << "Sum is: " << sum << endl;
return 0;
}2、编写一个C++程序,提示用户输入半径,并计算并输出圆的周长和面积。
#include <iostream>
using namespace std;
int main() {
float radius, circumference, area;
cout << "Enter radius of the circle: ";
cin >> radius;
circumference = 2 * 3.14 * radius;
area = 3.14 * radius * radius;
cout << "Circumference of the circle is: " << circumference << endl;
cout << "Area of the circle is: " << area << endl;
return 0;
}3、编写一个C++程序,提示用户输入三个数字,计算并输出这些数字的平均值。
#include <iostream>
using namespace std;
int main() {
float num1, num2, num3, average;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
average = (num1 + num2 + num3) / 3;
cout << "Average is: " << average << endl;
return 0;
}4、编写一个C++程序,使用循环计算并输出1到10的所有奇数。
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 10; i += 2) {
cout << i << " ";
}
cout << endl;
return 0;
}5、编写一个C++程序,创建一个整数数组,将数组中的所有元素相加并输出结果。
#include <iostream>
using namespace std;
int main() {
int arr[] = {3, 5, 7, 2, 1};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
cout << "Sum is: " << sum << endl;
return 0;
}6、编写一个C++程序,创建一个名为Rectangle的类,该类具有长度和宽度属性,以及计算矩形面积的方法。
#include <iostream>
using namespace std;
class Rectangle {
public:
float length;
float width;
float area() {
return length * width;
}
};
int main() {
Rectangle rect;
rect.length = 5;
rect.width = 3;
cout << "Area of the rectangle is: " << rect.area() << endl;
return 0;
}7、编写一个C++程序,创建一个名为Person的类,该类具有姓名、年龄和性别属性,以及输出个人信息的方法。
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string name;
int age;
char gender;
void info() {
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Gender: " << gender << endl;
}
};
int main() {
Person person;
person.name = "Alice";
person.age = 25;
person.gender = 'F';
person.info();
return 0;
}8、编写一个程序,使用结构体存储一个人的基本信息,包括姓名、年龄、性别和电话号码。要求使用指针访问和修改结构体成员的值,并能够输出修改后的结果。
#include <iostream>
#include <string>
using namespace std;
struct Person {
string name;
int age;
char gender;
string phone;
};
int main() {
Person person = {"Alice", 25, 'F', "123456789"};
Person *p = &person;
cout << "Name: " << p->name << endl;
cout << "Age: " << p->age << endl;
cout << "Gender: " << p->gender << endl;
cout << "Phone: " << p->phone << endl;
p->phone = "987654321";
cout << "New phone: " << p->phone << endl;
return 0;
}9、编写一个程序,动态分配一个大小为10的整数数组,并使用指针访问和修改数组元素的值。
#include <iostream>
using namespace std;
int main() {
int *p = new int[10]; // 动态分配一个大小为10的整数数组
for (int i = 0; i < 10; i++) {
p[i] = i + 1;
}
for (int i = 0; i < 10; i++) {
cout << "Value at index " << i << ": " << p[i] << endl;
}
delete[] p; // 释放动态分配的数组内存
return 0;
}
- 上一篇:C++输出1~100之间所有偶数
- 下一篇:python编程教程(小牛教编程)
