点类
先定义一个点类,类名为point,用课堂教师演示的方式添加类,即类的定义要在头文件中,另外有一个描述类成员函数实现的cpp文件,还有一个主函数的文件。
1)将其三维坐标定义为私有成员,通过构造函数为其初始化,并在构造函数和析构函数中有输出语句,以便于从运行结果看出构造函数析构函数的运行。
2)写三个构造函数用于重载,包含一个默认构造函数。
3)定义一个对象指针,并通过该指针完成对点对象坐标的输入和输出。
4)定义对象数组,观察构造函数和析构函数调用的顺序。
//源文件
#include<iostream>
using namespace std;
#include"point.h"
int main()
{
/*point* m=NULL;
m->get(2, 3, 4,0);*/
point p[3] = {point(1,2,3,1),point(2,3,4,2),point(5,6,7,3)};
}
//point.h
#pragma once
#include<iostream>
using namespace std;
class point
{
private:
int x, y, z;
int num;
public:
point();
point(int m_x, int m_y, int m_z,int m_num);
void get(int x, int y, int z,int num)
{
this->x = x;
this->y = y;
this->z = z;
this->num = num;
}
~point();
};
//point.cpp
#include "point.h"
point::point()
{
cout << "默认函数的调用" << endl;
}
point::point(int m_x, int m_y, int m_z,int m_num):x(m_x),y(m_y),z(m_z),num(m_num)
{
cout << "NO." << num << "有参函数的调用" << endl;
}
point::~point()
{
cout<<"NO."<<num << "析构函数的调用" << endl;
}
复数类
对于复数类(老师例子或作业),定义复数类的一个友元函数 complex add(complex x,complex y),用于完成两个复数的加法,对于减法、除法、乘法类似。
#include<iostream>
using namespace std;
class complex
{
friend complex add(complex x, complex y);
private:
int real, xu;
public:
complex(){}
complex(int x,int y):real(x),xu(y){}
void show()
{
cout << real << "+" << xu << "i" << endl;
}
~complex(){}
};
complex add(complex x, complex y)
{
complex temp;
temp.real = x.real + y.real;
temp.xu = x.xu + y.xu;
return temp;
}
int main()
{
complex c1(1, 2), c2(2, 3),c3;
c3 = add(c1, c2);
c3.show();
}
交通工具类
#include<iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(){}
Vehicle(const Vehicle &p){}
void set(int x, int y)
{
height = x;
max = y;
}
int gexw()
{
return height;
}
int getmax()
{
return max;
}
void pan(Vehicle &m)
{
if (this->height + m.height > this->max)
{
cout << "加入后超重警告" << endl;
}
else
{
cout << "没有超重" << endl;
}
}
void pan()
{
if (height > max)
{
cout << "超重警告" << endl;
}
}
private:
int height;
int max;
};
int main()
{
Vehicle h,a,b;
h.set(100, 200);
a.set(160, 100);
b.set(30, 100);
h.pan(a);
h.pan(b);
a.pan();
}
日期类
定义一个日期类Date,私有数据成员有:int型变量year, month, day。公有函数成员有:
1)三个形参均有默认值的构造函数,年月日的默认值依次为1000,1,1;
2)int isleap()判断year是否为闰年,若是返回1,否则返回0;
3)int check()判断日期是否合法,若合法返回1,否则返回0;
4)void setdate()设置year,month,day的值;
5)void display()按 “年-月-日”的格式输出日期,判断是否合法,若不合法输出Error Date,若合法,输出是否是闰年的信息;
6)(附加)void calendarprint()函数打印当月日历,格式为(以2014年4月日历为例)
Sun Mon Tue Wed Thu Fri Sta
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29
在主函数中任意输入年、月、日的值,定义日期类的对象4个,分别为d1(无参数),d2(年), d3(年,月), d4(年,月,日) 调用display()函数将4个日期信息进行输出,再输入一组年、月、日的值,对d1进行设置并调用display()函数输出日期信息
【输入形式】
年、月、日值(任意两个)
【输出形式】
年-月-日,若日期设置错误则输出Error,否则输出平年或者闰年信息
【输入样例】
2008 9 6 2008 2 30
【输出样例】
1000-1-1,Common year
2008-1-1,Leap year
2008-9-1,Leap year
2008-9-6,Leap year
2008-2-30,Error Date
2012 5 9 2012 2 29
1000-1-1,Common year
2012-1-1,Leap year
2012-5-1,Leap year
2012-5-9,Leap year
2012-2-29,Leap year
请按任意键继续. . .
2100 2 28 2100 2 29
1000-1-1,Common year
2100-1-1,Common year
2100-2-1,Common year
2100-2-28,Common year
2100-2-29,Error Date
请按任意键继续. . .
2000 13 31 2000 12 31
1000-1-1,Common year
2000-1-1,Leap year
2000-13-1,Error Date
2000-13-31,Error Date
2000-12-31,Leap year
请按任意键继续. . .
2001 6 31 2010 -5 -6
1000-1-1,Common year
2001-1-1,Common year
2001-6-1,Common year
2001-6-31,Error Date
2010--5--6,Error Date
请按任意键继续. . .
#include<iostream>
using namespace std;
class Date
{
private:
int year, month, day;
public:
Date(int y = 1000, int m = 1, int n = 1);
int isleap();
int check();
void setdate();
void display();
};
Date::Date(int y, int m, int d)
{
year = y;
month= m;
day = d;
}
int Date::isleap()
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
return 1;
}
else
{
return 0;
}
}
int Date::check()
{
if (month > 12 || month < 1 || day>31)
{
return 0;
}
return 1;
}
void Date::setdate()
{
cin >> year >> month >> day;
}
void Date::display()
{
cout << year << "-" << month << "-" << day<<" ";
if (!check())
{
cout << "Error Date" << endl;
}
else
{
if (isleap()) {
cout << "Leap year" << endl;
}
else
{
cout<< "Common year" << endl;
}
}
}
int main()
{
Date d1;
Date d2(2008);
Date d3(2023, 12, 22);
Date d4(2022, 13, 5);
d1.display();
d2.display();
d3.display();
d4.display();
}