请写一个示例C++程序,教我使用类
时间: 2024-02-29 19:51:23 浏览: 79
好的,以下是一个示例C++程序,用于教你如何使用类:
```cpp
#include <iostream>
using namespace std;
// 定义一个名为Person的类
class Person {
public:
// 定义类的构造函数
Person(string name, int age) {
this->name = name;
this->age = age;
}
// 定义类的成员函数
void sayHello() {
cout << "Hello, my name is " << name << ", and I am " << age << " years old." << endl;
}
private:
string name;
int age;
};
// 主函数
int main() {
// 创建一个名为Tom的Person对象
Person Tom("Tom", 20);
// 调用Tom的成员函数sayHello
Tom.sayHello();
return 0;
}
```
这个程序定义了一个名为Person的类,它有两个私有成员变量name和age,以及一个公有成员函数sayHello,用于输出对象的信息。在主函数中,我们创建了一个名为Tom的Person对象,并调用了它的sayHello函数输出信息。
相关问题
请写一个C++示例,教我如何使用虚函数
好的,以下是一个简单的C++示例,演示如何使用虚函数:
```c++
#include <iostream>
using namespace std;
class Shape {
protected:
int width, height;
public:
Shape( int a = 0, int b = 0) {
width = a;
height = b;
}
virtual int area() {
cout << "Parent class area :" <<endl;
return 0;
}
};
class Rectangle: public Shape {
public:
Rectangle( int a = 0, int b = 0):Shape(a, b) { }
int area() {
cout << "Rectangle class area :" <<endl;
return (width * height);
}
};
int main() {
Shape *shape;
Rectangle rec(10,7);
shape = &rec;
// 存储矩形的地址
shape->area();
return 0;
}
```
在上面的示例中,我们定义了一个`Shape`类,它有一个虚函数 `area()`,并且有一个子类 `Rectangle`,它重写了 `Shape` 类中的虚函数 `area()`。
在 `main()` 函数中,我们声明了一个 `Shape` 类型的指针 `shape`,并将它指向 `Rectangle` 类的对象 `rec`。然后,我们调用 `shape->area()` 函数,由于 `area()` 是虚函数,因此程序会根据 `rec` 对象的实际类型(即 `Rectangle`)来调用相应的函数。
因此,程序会输出:
```
Rectangle class area :
```
这个示例演示了虚函数的基本用法,它允许使用基类指针来调用派生类中的函数,从而实现多态性。
写一个C++程序,教我使用new 申请结构体数组,并为其中的元素赋予初始值
下面是一个示例程序,演示了如何使用`new`关键字动态分配结构体数组,并为其中的元素赋予初始值:
```c++
#include <iostream>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
int size;
cout << "请输入学生人数:";
cin >> size;
// 动态分配结构体数组
Student* students = new Student[size];
// 为每个元素赋初值
for (int i = 0; i < size; i++) {
students[i].name = "学生" + to_string(i + 1);
students[i].age = 18 + i;
}
// 输出每个元素的值
for (int i = 0; i < size; i++) {
cout << "学生" << i + 1 << "的姓名为:" << students[i].name << ",年龄为:" << students[i].age << endl;
}
// 释放动态分配的内存
delete[] students;
return 0;
}
```
在这个示例程序中,我们先从用户输入学生人数,然后使用`new`关键字动态分配了一个大小为`size`的结构体数组`students`。接着,我们使用循环为每个元素赋初值,即将每个学生的姓名赋值为`"学生1"`、`"学生2"`、`"学生3"`,以此类推,年龄赋值为`18`、`19`、`20`,以此类推。最后,我们再次循环输出每个学生的姓名和年龄,然后使用`delete[]`关键字释放动态分配的内存。
值得注意的是,在使用`new`关键字动态分配内存后,我们需要手动使用`delete[]`关键字释放动态分配的内存,以避免内存泄漏。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)