- 代码
- 1、编写.h文件
- 2、编写.cpp文件
- 3、编写main.cpp文件
- 运行
- Clion控制台乱码解决方案
#include
#include
using namespace std;
class Date
{
public:
//有参构造
Date(int year = 0, int month = 0, int day = 0);
//拷贝构造函数
Date(Date &birth);
//析构函数
~Date(){}
//输出出生日期
void showBirth();
private:
int m_year;
int m_month;
int m_day;
};
class Student
{
public:
/*
* 其他类对象作为本类数据成员时,其他类对象构造函数先调用,再调用本类构造函数,析构函数调用顺序相反
*/
//有参构造
Student(string name, int age, char gender, Date birth);
//析构函数
~Student(){}
//拷贝构造函数
Student(Student &stu);
//输出学生信息
void showStudent();
private:
string m_name; //姓名
int m_age; //年龄
char m_gender; //性别
Date m_birth; //出生日期
};
2、编写.cpp文件
#include "../Headers/test.h"
Date::Date(int year, int month, int day) {
m_year = year;
m_month = month;
m_day = day;
}
void Date::showBirth() {
cout<<"出生日期:"<<m_year<<"."<<m_month<<"."<<m_day<<endl;
}
Date::Date(Date &birth)
{
m_year = birth.m_year;
m_month = birth.m_month;
m_day = birth.m_day;
}
Student::Student(string name, int age, char gender, Date birth):m_birth(birth)
{
m_name = name;
m_age = age;
m_gender = gender;
}
Student::Student(Student &stu)
{
m_name = stu.m_name;
m_age = stu.m_age;
m_gender = stu.m_gender;
}
void Student::showStudent()
{
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
cout<<"姓名:"<<m_name<<endl;
if(m_gender == 'f')
cout<<"性别:"<<"女"<<endl;
else
cout<<"性别:"<<"男"<<endl;
cout<<"年龄:"<<m_age<<endl;
m_birth.showBirth();
cout<<"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
}
3、编写main.cpp文件
#include "test.cpp"
int main() {
int year, month, day; //出生日期
string name; //姓名
char gender; //性别
int age; //年龄
cout << "请输入出生日期:";
cin >> year >> month >> day;
cout << "请输入姓名:";
cin >> name;
cout << "请输入年龄:";
cin >> age;
cout << "请输入性别(f(女)/m(男)):";
do
{
cin>>gender;
}while(gender != 'f' && gender != 'm');
Date birth(year, month, day);
Student stu(name, age, gender, birth);
stu.showStudent();
}
运行
Clion控制台乱码解决方案
在cmakelist.txt添加如下代码
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)