实验二-类与对象

实验二-类与对象

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() {}//没有返回类型

private:
	int m;
};

int main()
{
	A a(5);
	a.m+=10;
	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() <<" " << array[1].getfloat()<< 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);
	cout << "基本工资=" << Z3.Get_Wage()<<endl;
	Z3.Set_Wage(4000.258);
	cout << "基本工资=" << Z3.Get_Wage() << endl << "实际发放工资=" << 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() << ends;
	cout << "总销售款为" << sum << ends;
    cout << "总销售件数为" << n << ends; 

}
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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁芜~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值