0
点赞
收藏
分享

微信扫一扫

实验二-类与对象

朱悟能_9ad4 2022-04-13 阅读 34
c++

实验二-类与对象

1.

程序1

#include<iostream>
using namespace std;
class Date
{public:
	void set_date();
	void show_date();
private:
	int year;
	int month;
	int day;
};
Date d;
void Date::set_date()
{
	cin >> d.year;
	cin >> d.month;
	cin >> d.day;
}
void Date::show_date()
{
	cout << d.year <<  "/" << d.month <<"/" << d.day << endl;
}
int main()
{
	d.set_date();
	d.show_date();
}

程序3

#include<iostream>
using namespace std;
class X
{
private:
	int a;//类里面不能直接初始化
	int& b;
	const int c;//为常数据成员,只能通过构造函数参数列表进行初始化
public:	
	void setA(int i) { a = i; }
	X(int i,int &j,int k=0):a(i),b(j),c(k){}
    X() { a = b = 0; }
	X(int i, int j, int k):a(i), b(j), c(k){}//改为参数列表才可以初始化c
	//void setC(int k) const 
	//{ c = c + k; }///c是常数据变量,不能改变
};
int main()
{
	X x1;
	X x2(2);
	X x3(1, 2, 3);
	x1.setA(3);
	return 0;
}

程序2

#include<iostream>
using namespace std;
class A
{
public:
	A(int i = 0) { m = i; }//构造函数没有返回类型
	void show() { cout << m << endl; }
	 ~A() {}析构函数没有返回类型
	 int Get_m();
private:
	int m;
};
int A::Get_m()
{
	m += 10;
	return m;

}//返回m的值
int main()
{
	A a(5);
	a.Get_m();
	a.show();
	return 0;
}

2.

#include<iostream>
using namespace std;
class test {
public:
	test();
	int getint() { return num; }
	float getfloat() { return f1; }
	~test();
private:
	int num;
	float f1;
};
test::test()
{
	cout << "Initalizing default" << endl;
	num = 0; f1 = 0.0;
}
test::~test()
{
	cout << "Destructor is active" << endl;
}
int main()
{
	test array[2];
	cout << array[1].getint() <<" "<< "arrr[1]输出完成" << array[1].getfloat()<<"arrr[2]输出完成" << endl;
	return 0;
}

3.

#include<iostream>
using namespace std;
class Salary
{
private:
	double Wage, Subsidy, Rent, WaterFee, ElecFee;//基本工资,岗位补贴,房租,水费,电费
public:
	Salary() :Wage(0), Subsidy(0), Rent(0), WaterFee(0), ElecFee(0){}
	Salary(double a, double b, double c, double d, double e ) :Wage(a), Subsidy(b), Rent(c), WaterFee(d), ElecFee(e){}
	void Set_Wage(double a){Wage = a;}
	double Get_Wage(){return Wage;}
	void Set_Subsidy(double b) { Subsidy = b; }
	double Get_Subsidy() { return Subsidy; }
	void Set_Rent(double c) { Rent = c; }
	double Get_Rent() { return Rent; }
	void Set_WaterFee(double d) { WaterFee = d; }
	double Get_WaterFee() { return WaterFee; }
	void Set_ElecFee(double e) { ElecFee = e; }
	double Get_ElecFee() { return ElecFee; }
	double RealSalary()
	{
		return  Wage + Subsidy - Rent - WaterFee - ElecFee;
	}

};
void main()
{
	Salary Z3;//工资清零
	Salary L4(4800, 50, 20, 45, 48);//L4实际工资与消费
	cout << "Z3基本工资=" << Z3.Get_Wage()<<endl;
	Z3.Set_Wage(4000.258);
	cout << "Z3实际基本工资=" << Z3.Get_Wage() << endl << "L4实际发放工资=" << L4.RealSalary() << "元";


}

4

#include "iostream"
using namespace std;
class intArray
{
public:
	intArray(int size);           //构造函数
	intArray(const intArray& x);  //拷贝构造函数
	~intArray();                //析构函数
	bool Set(int i, int elem);  //设置第i个数组元素的值,设置成功返回								true,失败返回false
	bool Get(int i, int& elem);   //获取第i个数组元素的值,获取成功									返回true,失败返回false
	int Length() const;//获取数组的长度
	void Resize(int size);   //重置数组
	void Print();    //输出数组
private:
	int* element;   //指向动态数组的指针
	int arraysize;  //数组的大小
};
intArray::intArray(int size)
{
	arraysize = size;
}
intArray::intArray(const intArray& x)
{
	element = x.element;
	arraysize = x.arraysize;
	cout << "拷贝成功" << endl;
}
intArray::~intArray()
{
	cout << "数组程序成功运行"<<endl;
}
bool intArray::Set(int i, int elem)
{
	*(element + i) = elem;
	if (!element)
		return false;
	else return true;

}
bool intArray::Get(int i, int &elem)
{
	return *(element + i);
}
void intArray::Resize(int size)
{
	arraysize = size;
}
void intArray::Print()
{
	for (int i = 0; i < arraysize; i++)

	{
		cout << *(element + i) << "ends";
	}
}
int intArray::Length() const
{
	return arraysize;
}






#include <iostream>
using namespace std;
class Discount
{
private:
	int num;//成员编号
	double price;//输入价格
	int quantity;//出售数量
	static int n;//出售总数量
	static double sum;//出售总价格
	static double discount;//折扣
public:
	void in();//输入数据
	void count();//处理总价格
	static double average();//平均价格
	static void display();//展示结果

};
double Discount::sum = 0;
double Discount::discount = 0.98;
int Discount::n = 0;
void Discount::in()//输入相关变量
{
	cout << "请输入售货员编码:";
	cin >> num;
	cout << endl << "请输入售货件数:";
	cin >> quantity;
	cout << endl << "请输入售货单价:";
	cin >> price;
	cout << endl;

}
void Discount::count()//计算总价格和出售总数量
{
	if (quantity > 10)
	{
		sum += quantity * price * discount;
		n += quantity;
	}
	else
	{
		sum += quantity * price;
		n += quantity;
	}
}
double Discount::average()//计算平均价格
{

	return sum / n;
}
void Discount::display()
{
	cout << "平均售价为" << Discount::average() << endl;
	cout << "总销售款为" << sum << endl;
    cout << "总销售件数为" << n << endl; 

}
int main()
{
	Discount d[3];
	for (int j = 0; j < 3; j++)
	{
		cout << "第" << j + 1 << "组:" << endl;
		d[j].in();
	}
	for (int i = 0; i < 3; i++)
	{
		d[i].count();
	}
	Discount::display();
	return 0;
}

举报

相关推荐

0 条评论