最简单的Hello World
#include<iostream>
int main() {
std::cout << "Hello World" << std::endl;
}
变量
#include<iostream>
int main() {
int a = 5;
float b = 3.14;//float 的精度低于 double
char c = 'A';
bool d = true;
std::cout << "变量: " << a << ", " << b << ", " << c <<", "<<d<< std::endl;
}
//变量: 5, 3.14, A, 1
运算
#include<iostream>
int main() {
int a = 5, b = 3;
std::cout << a + b; // 输出 8
std::cout << a - b; // 输出 2
std::cout << a * b; // 输出 15
std::cout << a / b; // 输出 1(整除)
std::cout << a % b; // 输出 2(取余)
}
逻辑运算符
#include<iostream>
int main() {
int a = 5, b = 3;
bool result = (a > b) && (a != 0); // 逻辑与
std::cout << result;//1
result = (a < b) || (a == 0); // 逻辑或
std::cout << result;//0
result = !result; // 逻辑非
}
if-else
#include<iostream>
int main() {
int a = 19;
// if-else 控制结构
if (a > 3) {
std::cout << "a 大于 3" << std::endl;
}
else
{
std::cout<<"a 小于 3" << std::endl;
}
}
//a 大于 3
for循环
#include<iostream>
int main() {
for (int i = 0; i < 3; ++i) {
std::cout << "循环第 " << i + 1 << " 次" << std::endl;
}
}
//循环第 1 次
//循环第 2 次
//循环第 3 次
while
#include<iostream>
int main() {
int i = 0;
while (i < 5) {
std::cout << i << " ";
i++;
}
}
do-while
#include<iostream>
//先执行循环,再检查条件
int main() {
int i = 0;
do {
std::cout << i << " ";
i++;
} while (i < 5);
}
switch
#include<iostream>
int main() {
int day = 2;
switch (day) {
case 1: std::cout << "Monday"; break;
case 2: std::cout << "Tuesday"; break;
default: std::cout << "Invalid day";
}
}
指针和引用
#include<iostream>
void pointerAndReference() {
std::cout << "== 指针与引用 ==" << std::endl;
int x = 10;
int* ptr = &x; // 指针,x的地址在指针ptr
int& ref = x; // 引用, `ref` 是变量 `x` 的别名
std::cout << "指针指向的值: " << *ptr << std::endl;
std::cout << "引用指向的值: " << ref << std::endl;
std::cout << "x的地址 " << ptr << std::endl;
ref = 20; // 改变引用也会改变 x
std::cout << "指针指向的值: " << *ptr << std::endl;
std::cout << "引用指向的值: " << ref << std::endl;
std::cout << "x: " << x << std::endl;
std::cout << "x的地址 " << ptr << std::endl;
*ptr = 30;
std::cout << "指针指向的值: " << *ptr << std::endl;
std::cout << "引用指向的值: " << ref << std::endl;
std::cout << "x: " << x << std::endl;
std::cout << "x的地址 " << ptr << std::endl;
}
int main() {
pointerAndReference();
}
//== 指针与引用 ==
//指针指向的值: 10
//引用指向的值 : 10
//x的地址 000000D08A8FF5A4
//指针指向的值 : 20
//引用指向的值 : 20
//x : 20
//x的地址 000000D08A8FF5A4
//指针指向的值 : 30
//引用指向的值 : 30
//x : 30
//x的地址 000000D08A8FF5A4
面向对象编程
-
封装(Encapsulation)
- 封装是将数据(属性)和操作这些数据的行为(方法)结合在一起,通过对象对外提供有限的接口,而隐藏对象内部的实现细节。
- 这样可以保护对象内部的数据不被外部直接修改,确保数据的完整性。
-
继承(Inheritance)
- 继承允许一个类(子类)从另一个类(父类)继承属性和方法,从而可以复用已有的代码,并在子类中扩展或修改父类的功能。
- 继承的好处是提高了代码的可重用性和可扩展性。
-
多态(Polymorphism)
- 多态指的是不同的对象在响应相同消息(方法调用)时,表现出不同的行为。多态可以通过函数重载或通过继承与虚函数来实现。
- 它允许我们通过父类指针或引用来调用子类的行为,实现运行时的灵活性。
-
抽象(Abstraction)
- 抽象是一种只保留对象的必要属性和方法,而隐藏其不必要的具体实现细节的过程。抽象通过接口或抽象类来实现,强调“做什么”而不是“怎么做”。
#include <iostream>
#include <string>
// 基类 Person
class Person {
protected:
std::string name;
int age;
public:
// 构造函数
Person(const std::string& name, int age) : name(name), age(age) {}
// 虚函数:实现多态
virtual void display() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
// 提供接口供外部修改和访问数据
void setName(const std::string& name) {
this->name = name;
}
std::string getName() const {
return name;
}
void setAge(int age) {
this->age = age;
}
int getAge() const {
return age;
}
};
// 子类 Student 继承自 Person
class Student : public Person {
private:
std::string major;
public:
// 构造函数:调用父类构造函数
Student(const std::string& name, int age, const std::string& major)
: Person(name, age), major(major) {}
// 覆盖父类的 display 函数(多态)
void display() const override {
std::cout << "Student Name: " << name << ", Age: " << age << ", Major: " << major << std::endl;
}
void setMajor(const std::string& major) {
this->major = major;
}
std::string getMajor() const {
return major;
}
};
// 子类 Teacher 继承自 Person
class Teacher : public Person {
private:
std::string subject;
public:
// 构造函数:调用父类构造函数
Teacher(const std::string& name, int age, const std::string& subject)
: Person(name, age), subject(subject) {}
// 覆盖父类的 display 函数(多态)
void display() const override {
std::cout << "Teacher Name: " << name << ", Age: " << age << ", Subject: " << subject << std::endl;
}
void setSubject(const std::string& subject) {
this->subject = subject;
}
std::string getSubject() const {
return subject;
}
};
// 主函数
int main() {
// 创建 Student 对象
Student student("Alice", 20, "Computer Science");
student.display();
// 创建 Teacher 对象
Teacher teacher("Bob", 45, "Mathematics");
teacher.display();
// 使用多态,父类指针指向子类对象
Person* person1 = &student;
Person* person2 = &teacher;
std::cout << "\nUsing Polymorphism:" << std::endl;
person1->display(); // 调用 Student 的 display
person2->display(); // 调用 Teacher 的 display
return 0;
}
//Student Name : Alice, Age : 20, Major : Computer Science
//Teacher Name : Bob, Age : 45, Subject : Mathematics
//
//Using Polymorphism :
//Student Name : Alice, Age : 20, Major : Computer Science
//Teacher Name : Bob, Age : 45, Subject : Mathematics
继承和多态
#include <iostream>
#include <string>
class Person {
public:
std::string name;
int age;
//构造函数
//也可表示
//Person(std::string name, int age):name(name),age(age){}
//1.//使用 const std::string& ,可以避免拷贝 ,高效
Person(const std::string& name, int age) {
this->name = name;
this->age = age;
}
//2.std::string name只是按值传递,可以更改
//Person(std::string name, int age) {
// this->name ="a"+ name;
// this->age = age;
//}
// 虚函数 display,常量成员函数,const 确保 display() 不会修改对象的成员变量
virtual void display() const{
// 基类中的虚函数
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
//继承
class student: public Person
{
public:
std::string major;
student(const std::string& name, int age, const std::string& major)
: Person(name, age), major(major) {} // 调用继承的构造函数
void display() const override {
// 派生类中重写虚函数
std::cout << "Name: " << name << ", Age: " << age << ",major:"<<major<<std::endl;
}
};
int main() {
Person person("Alice", 30);
student st("lihua", 20, "学生");
person.display();
st.display();
const Person* pPerson = &person;
const Person* pstudent = &st;
// 调用 display 函数,通过基类指针实现多态
pPerson->display(); // 调用 Person 类的 display
pstudent->display();
return 0;
}
模板编程
是 C++ 的一个核心特性,它允许编写通用代码,以适应多种类型。通过模板,代码可以在编译时根据实际使用的数据类型自动生成相应的函数或类实例,从而实现高效的代码复用和类型安全。
1. 函数模板
#include<iostream>
template<typename T>
T add(T a, T b) {
return a + b;
}
int main() {
std::cout << add(1, 2) << std::endl;// 使用 int 类型
std::cout << add(1.1, 2.2) << std::endl;// 使用 double 类型
std::cout << add<int>(1, 2) << std::endl;//指定使用 int 类型
}
//3
//3.3
//3
2. 类模板
#include <iostream>
template<typename T>
class Box {
private:
T value;
public:
Box(T val) : value(val) {}
T getValue() const {
return value;
}
};
int main() {
Box<int> intBox(100); // int 类型的 Box
Box<double> doubleBox(3.14); // double 类型的 Box
std::cout << intBox.getValue() << std::endl; // 输出 100
std::cout << doubleBox.getValue() << std::endl; // 输出 3.14
return 0;
}
3. 模板特化
#include <iostream>
// 通用模板
template<typename T>
class Box {
public:
T value;
Box(T v) : value(v) {}
void show() {
std::cout << "Value: " << value << std::endl;
}
};
// 对于 char 类型的特化
template<>
class Box<char> {
public:
char value;
Box(char v) : value(v) {}
void show() {
std::cout << "Character: " << value << std::endl;
}
};
int main() {
Box<int> intBox(123);
Box<char> charBox('A');
intBox.show(); // 输出: Value: 123
charBox.show(); // 输出: Character: A
return 0;
}