一、简述题
拷贝构造函数通常在以下情况下被调用:
class Tree {
private:
int age; // 树龄
public:
// 构造函数,初始化树的年龄
Tree(int initialAge) : age(initialAge) {}
// 成长函数,计算经过year年后的树的最新年龄
void grow(int years) {
age += years;
}
// 获取树的年龄
int getAge() {
return age;
}
};
#include <iostream>
class Integer {
private:
int value; // 整型数值
public:
// 构造函数,用于初始化整数值
Integer(int v) : value(v) {}
// 显示Integer对象的值
void displayValue() {
std::cout << "The value is: " << value << std::endl;
}
// 将两个Integer对象相加
Integer add(const Integer& other) {
return Integer(value + other.value);
}
};
#include <iostream>
class Integer {
private:
int value; // 整型数值
public:
// 构造函数,用于初始化整数值
Integer(int v) : value(v) {}
// 显示Integer对象的值
void displayValue() {
std::cout << "The value is: " << value << std::endl;
}
// 将两个Integer对象相加
Integer add(const Integer& other) {
return Integer(value + other.value);
}
};
#include <iostream>
class Circle {
private:
double radius; // 半径
public:
// 构造函数,用于初始化半径
Circle(double r) : radius(r) {}
// 获取半径值
double getRadius() {
return radius;
}
// 计算周长
double calculatePerimeter() {
return 2 * 3.14159 * radius;
}
// 计算面积
double calculateArea() {
return 3.14159 * radius * radius;
}
// 输出圆的相关信息
void printInfo() {
std::cout << "Circle Information:" << std::endl;
std::cout << "Radius: " << radius << std::endl;
std::cout << "Perimeter: " << calculatePerimeter() << std::endl;
std::cout << "Area: " << calculateArea() << std::endl;
}
};
#include <iostream>
class Score {
private:
static int totalScore; // 静态数据成员,存储总分
public:
// 静态成员函数,实现对静态数据成员的访问
static int GetTotalScore() {
return totalScore;
}
};
// 静态数据成员的定义和初始化
int Score::totalScore = 0;
int main() {
// 假设有一些学生成绩需要统计
int scores[] = {80, 90, 75, 85, 95};
// 统计总分
for (int i = 0; i < 5; i++) {
Score::totalScore += scores[i];
}
// 输出总分
std::cout << "Total score: " << Score::GetTotalScore() << std::endl;
return 0;
}
#include <iostream>
class Time {
private:
int hours;
int minutes;
int seconds;
public:
// 默认构造函数,将时间初始化为0
Time() : hours(0), minutes(0), seconds(0) {}
// 带参数的构造函数,将时间初始化为给定值
Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) {}
// 显示时间在屏幕上
void displayTime() {
std::cout << "Time: " << hours << ":" << minutes << ":" << seconds << std::endl;
}
};
#include <iostream>
class Time {
private:
int hours;
int minutes;
int seconds;
public:
// 默认构造函数,将时间初始化为0
Time(int h = 0, int m = 0, int s = 0) : hours(h), minutes(m), seconds(s) {}
// 显示时间在屏幕上
void displayTime() {
std::cout << "Time: " << hours << ":" << minutes << ":" << seconds << std::endl;
}
};
#include <iostream>
#include <string>
// 父类 Person
class Person {
protected:
int id;
std::string name;
public:
void inputPersonData() {
std::cout << "Enter ID: ";
std::cin >> id;
std::cout << "Enter Name: ";
std::cin >> name;
}
void displayPersonData() {
std::cout << "ID: " << id << std::endl;
std::cout << "Name: " << name << std::endl;
}
};
// 子类 Student
class Student : public Person {
private:
int classNumber;
int score;
public:
void inputStudentData() {
inputPersonData();
std::cout << "Enter Class Number: ";
std::cin >> classNumber;
std::cout << "Enter Score: ";
std::cin >> score;
}
void displayStudentData() {
displayPersonData();
std::cout << "Class Number: " << classNumber << std::endl;
std::cout << "Score: " << score << std::endl;
}
};
// 子类 Teacher
class Teacher : public Person {
private:
std::string title;
std::string department;
public:
void inputTeacherData() {
inputPersonData();
std::cout << "Enter Title: ";
std::cin >> title;
std::cout << "Enter Department: ";
std::cin >> department;
}
void displayTeacherData() {
displayPersonData();
std::cout << "Title: " << title << std::endl;
std::cout << "Department: " << department << std::endl;
}
};
int main() {
Student student;
Teacher teacher;
std::cout << "Enter Student Data:" << std::endl;
student.inputStudentData();
std::cout << std::endl;
std::cout << "Enter Teacher Data:" << std::endl;
teacher.inputTeacherData();
std::cout << std::endl;
std::cout << "Student Data:" << std::endl;
student.displayStudentData();
std::cout << std::endl;
std::cout << "Teacher Data:" << std::endl;
teacher.displayTeacherData();
return 0;
}