利用c++编程:构造一个类student,实现求平均成绩
时间: 2024-01-31 11:04:10 浏览: 31
好的,下面是实现求平均成绩的C++代码:
```c++
#include <iostream>
using namespace std;
class Student {
private:
int score1, score2, score3; // 三门课的分数
public:
Student(int s1, int s2, int s3) {
score1 = s1;
score2 = s2;
score3 = s3;
}
float getAverageScore() {
return (score1 + score2 + score3) / 3.0;
}
};
int main() {
Student s(90, 80, 85);
cout << "平均成绩:" << s.getAverageScore() << endl;
return 0;
}
```
这里定义了一个`Student`类,包含了三门课的成绩,以及一个求平均成绩的方法`getAverageScore()`。在`main()`函数中创建了一个`Student`对象`s`,并调用`getAverageScore()`方法求出平均成绩并输出。
相关问题
c++编程 定义一个学生类Student(属性有姓名name和学号number),打印输出某个学生的学生信息,要求用一个友元函数Show( )函数实现。
### 回答1:
以下是一个示例代码:
```c++
#include <iostream>
#include <string>
class Student {
private:
std::string name;
int number;
public:
Student(std::string name, int number) : name(name), number(number) {}
friend void Show(Student& student);
};
void Show(Student& student) {
std::cout << "Name: " << student.name << std::endl;
std::cout << "Number: " << student.number << std::endl;
}
int main() {
Student s("Tom", 12345);
Show(s);
return 0;
}
```
在上面的代码中,我们定义了一个 `Student` 类,它有两个私有属性,分别是姓名和学号。我们使用构造函数来初始化这两个属性。然后,我们定义了一个友元函数 `Show`,它可以访问 `Student` 类的私有属性。在 `main` 函数中,我们创建了一个 `Student` 对象 `s`,并调用 `Show` 函数来打印输出学生信息。
### 回答2:
学生类的定义如下:
```cpp
#include <iostream>
using namespace std;
// 学生类的定义
class Student{
private:
string name;
string number;
public:
// 构造函数
Student(string name, string number){
this->name = name;
this->number = number;
}
// 友元函数的声明
friend void Show(Student student);
};
// 友元函数的实现
void Show(Student student){
cout << "学生姓名:" << student.name << endl;
cout << "学号:" << student.number << endl;
}
int main(){
// 创建一个学生对象
Student student("张三", "1001");
// 调用友元函数打印输出学生信息
Show(student);
return 0;
}
```
以上代码定义了一个学生类`Student`,包含了姓名和学号两个私有属性,通过构造函数进行初始化。然后定义了一个友元函数`Show`,通过引用传递一个`Student`对象,打印输出该学生的姓名和学号信息。在`main`函数中,创建一个学生对象,并调用友元函数`Show`进行输出学生信息。运行程序,即可打印输出学生的信息。
### 回答3:
学生类Student可以定义如下:
```cpp
#include<iostream>
using namespace std;
class Student {
private:
string name; // 学生姓名
int number; // 学生学号
public:
Student(string n, int num) : name(n), number(num) {} // 构造函数
friend void Show(Student s); // 声明友元函数
};
void Show(Student s) {
cout << "学生姓名:" << s.name << endl;
cout << "学生学号:" << s.number << endl;
}
int main() {
Student stu("张三", 123456); // 创建一个学生对象
Show(stu); // 调用友元函数显示学生信息
return 0;
}
```
上述代码中,我们首先定义了一个学生类Student,其中包含了私有属性name和number,以及一个公有的构造函数,用于初始化学生对象的姓名和学号。在类的外部,我们通过friend关键字声明了Show函数为Student类的友元函数,这样Show函数就能够访问Student类的私有成员。
在main函数中,我们创建了一个学生对象stu,并传入了姓名"张三"和学号123456进行初始化。然后调用Show函数,将stu作为参数传递给该函数,显示学生的姓名和学号信息。最终输出结果如下:
```
学生姓名:张三
学生学号:123456
```
C++实现一个复杂的类,包括所有C++的知识点
一个复杂的类可以包括以下C++知识点:
1. 构造函数和析构函数
2. 静态成员变量和静态成员函数
3. 友元函数和友元类
4. 继承和多态
5. 运算符重载
6. 模板和泛型编程
7. 异常处理
8. RAII(资源获取即初始化)
下面是一个示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Person {
public:
// 构造函数
Person(string name, int age) : m_name(name), m_age(age) {
cout << "Person constructor called" << endl;
}
// 析构函数
~Person() {
cout << "Person destructor called" << endl;
}
// 静态成员变量和静态成员函数
static int s_count;
static void s_func() {
cout << "Static function called" << endl;
}
// 友元函数和友元类
friend void friend_func(Person& p) {
cout << "Friend function called, name: " << p.m_name << endl;
}
friend class FriendClass;
// 继承和多态
virtual void virtual_func() {
cout << "Person virtual function called" << endl;
}
// 运算符重载
bool operator<(const Person& other) const {
return m_age < other.m_age;
}
// 模板和泛型编程
template <typename T>
void template_func(vector<T>& vec) {
for (auto& v : vec) {
cout << v << " ";
}
cout << endl;
}
// 异常处理
void throw_exception() {
throw runtime_error("Exception thrown");
}
// RAII(资源获取即初始化)
class Resource {
public:
Resource() {
cout << "Resource acquired" << endl;
}
~Resource() {
cout << "Resource released" << endl;
}
};
private:
string m_name;
int m_age;
};
// 静态成员变量初始化
int Person::s_count = 0;
// 继承和多态
class Student : public Person {
public:
Student(string name, int age, string school) : Person(name, age), m_school(school) {
cout << "Student constructor called" << endl;
}
~Student() {
cout << "Student destructor called" << endl;
}
void virtual_func() override {
cout << "Student virtual function called" << endl;
}
private:
string m_school;
};
// 友元类
class FriendClass {
public:
void friend_class_func(Person& p) {
cout << "Friend class function called, age: " << p.m_age << endl;
}
};
int main() {
// 构造函数和析构函数
Person p("Tom", 20);
// 静态成员变量和静态成员函数
Person::s_count = 10;
Person::s_func();
// 友元函数和友元类
friend_func(p);
FriendClass fc;
fc.friend_class_func(p);
// 继承和多态
Person* ptr = new Student("Jerry", 18, "MIT");
ptr->virtual_func();
delete ptr;
// 运算符重载
vector<Person> vec{Person("A", 10), Person("B", 30), Person("C", 20)};
sort(vec.begin(), vec.end());
for (auto& v : vec) {
cout << v.m_name << " ";
}
cout << endl;
// 模板和泛型编程
vector<int> int_vec{1, 2, 3};
vector<string> str_vec{"hello", "world"};
p.template_func<int>(int_vec);
p.template_func<string>(str_vec);
// 异常处理
try {
p.throw_exception();
} catch (const exception& e) {
cout << e.what() << endl;
}
// RAII(资源获取即初始化)
{
Person::Resource res;
}
return 0;
}
```