实验二-类与对象

实验二-类与对象

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
void Bus_link::input() { Bus_infor *p,*p2=NULL; p=head; int n; while(p->next) p=p->next; while(n) { p2=new Bus_infor; p2->input(); p->next=p2; p2->next=NULL; p=p->next; Bus_link::setkey(1); cout<<"\t\t\t按1继续,按0返回:"; cin>>n; } } #include<iostream>//数据流输入/输出 #include<fstream>//文件输入/输出 #include<string>//字符串操作 #include<iomanip>//参数化输入/输出 #include<time.h>//时间库函数 usingnamespace std; //命名空间 class Bus_infor { private: staticint Bus_No; //静态数据成员,统计当前所有的Bus_infor对象的数目 char start[20]; //起点站 char end[20]; //终点站 int Bus_order; //班次号 int all_tickted; //额定载量 int tickted; //已定票人数 int Hour_start,Minute_start; //发车时间 float GoHour; //行车时间 public: Bus_infor(); ~Bus_infor(); Bus_infor *next; void input(); //录入函数 void input(ifstream & is); //读取函数 void output(); //输出函数 void output(ofstream & os); //写入函数 void Order_tickt(int n); //定票函数 void Unorder_tickt(int n); //退票函数 void GetTime_start(); //获取发车时间函数 bool GetTime(); //判断当前班次状况函数 int Get_all_tickted() { return all_tickted; } //联函数,返回额定载量 int Get_tickted() { return tickted; } //返回已定票人数 int Get_bus_order() { return Bus_order; } //返回班次号 string Get_end()const; //返回终点站的字符串 }; int Bus_infor::Bus_No=1; Bus_infor::Bus_infor() { Bus_No++; tickted=0; } Bus_infor::~Bus_infor() { Bus_No--; } void Bus_infor::input() { cout<<"\t\t\t按提示输入:"<<endl; cout<<"输入班次: "; while(1) { cin>>Bus_order; if (cin.fail()) //判断输入的数据类型是否有错 { cout << "\n班次输入错误,请重新输入:"; cin.clear(); cin.get(); } else break; } cout<<"请输入车的额定座位数: "; while(1) { cin>>all_tickted; if (cin.fail()) //判断输入的数据类型是否有错 { cout << "\n座位数输入错误,请重新输入:"; cin.clear(); cin.get(); } else break; } GetTime_start(); cout<<"请输入行车时间:"; while(1) { cin>>GoHour; if (cin.fail()) //判断输入的数据类型是否有错 { cout << "\n行车时间输入错误,请重新输入:"; cin.clear(); cin.get(); } else break; } cout<<"请输入起始站与终点站:"; cin>>start;cin>>end; cout<<"是否清空售票(y/n)?"; char a;cin>>a; if(a=='y'""a=='Y') tickted=0; } void Bus_infor::input(ifstream & is) { is>>Bus_order>>Hour_start>>Minute_start>>start>>end>>GoHour>>all_tickted>>ti ckted; is.get(); } void Bus_infor:
一.阅读程序题 1. #include class CSample { int i; public: CSample() { i=1; cout<<i++; } void disp() { cout<<++i; } ~CSample() { cout<<i++; } }; void main() { CSample s; s.disp(); } 2. #include class Sample { private: int x; public: Sample(){ cout<<(x=0);} Sample(int i,int j) {cout<<(x=i+j);} ~Sample(){cout<<x;} }; void main() { Sample *p1=new Sample, *p2=new Sample(3,4); delete p1; delete p2; } 3. #include class Sample { public: Sample(){} Sample(int a){x=a;} Sample(Sample &a){x=a.x++;} void disp(){cout<<x++;} private: int x; }; void fun(Sample &s1, Sample s2) { s1.disp ();s2.disp (); } void main() { Sample s1(2),s2=s1; fun(s2,s1); s1.disp ();s2.disp (); } 4. #include class Sample { int x; public: Sample(){x=0;} Sample(int a) {cout<<(x=a);} ~Sample() { cout<<++x; } void disp() { cout<<x; } }; void main() { Sample s1(2); s1.disp (); s1.~Sample (); } 5. #include class Sample { int x; public: Sample(){cout<<(x=0);} Sample(int i){cout<<(x=i);} ~Sample(){cout<<x<<endl;} void disp(){cout<<x;} }; void main() { Sample s(3); int i=0; if(i=0) { Sample s; s.disp (); } } 6. #include class Sample { public: Sample(){cout<<"Constructor"<<endl;} ~Sample(){cout<<"Destructor"<<endl;} }; void fn(int i) { static Sample c; cout<<"i="<<i<<endl; } void main() { fn(10); fn(20); } 7. #include class Sample { int n; public: Sample(int i){n=i;} friend int add(Sample &s1,Sample &s2); }; int add(Sample &s1,Sample &s2) { return s1.n+s2.n; } void main() { Sample s1(10),s2(20); cout<<add(s1,s2)<<endl; } 8. #include class B; class A { int i; public: int set(B&); int get(){return i;} A(int x){i=x;} }; class B { int i; public: B(int x){i=x;} friend A; }; int A::s
ActiveMQ 使用例子,包含静态库和头文件,VS2015 编译。 // testActiveMQ.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" /* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ // START SNIPPET: demo /*extern "C" {*/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include //} #include #include #include #include using namespace activemq::core; using namespace decaf::util::concurrent; using namespace decaf::util; using namespace decaf::lang; using namespace cms; using namespace std; #pragma comment (lib, "apr-1.lib") #pragma comment (lib, "aprutil-1.lib") #pragma comment (lib, "libapriconv.lib") #pragma comment (lib, "libactivemq-cpp.lib") #ifdef _WIN32 #pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"Mswsock.lib") #pragma comment(lib,"Rpcrt4

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

繁芜~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值