C++学习记录与心得(一)类和对象

  • Post author:
  • Post category:其他


一、写在开头

记录自己的学习过程也是为自己的学习生涯做一点标记,既可以方便以后查阅,对我本人来说也可以起到促进学习的作用。本人之前会一点c语言,现在跟着课程向导学习一点c++的基本知识。笔者自身也是处于学习之中,如果有什么不对的地方希望大家不吝赐教。

二、类和对象

1. c++与c不同的一点就是C++中有类和对象之说,public中定义公用的数据和成员函数,private下定义私有的数据和成员函数。

公用的成员(public)既可以被类中引用也可以被类外引用,而私有的(private)只能被类内所引用。一般希望能被外界调用的成员指定为公用的,相当于类对外的接口。

而有些函数不希望被外界所调用,定义为私有的类型。

#include<iostream>
using namespace std ;
class Time {
public:
	void settime() { //在类内定义成员函数
		cin >> hour;
		cin >>minute;
		cin >> sec;
	}
	void showtime() {
		cout << hour << ":" << minute << ":" << sec << endl;
	}
private:
	int hour;
	int minute;
	int sec;
};
int main() {
	Time t1;
	t1.settime();
	t1.showtime();
	return 0;
}

这是课程中一段代码,用于输入输出时间。

2.需要求3个长方体的体积,请编一个基于对象的程序。数据成员包括length (长),width (宽),height(高),要求用成员函数实现以下的功能:键盘输入3个长方柱的长、宽、高;计算长方柱的体积;输出3个长方柱体积;

#include<iostream>
using namespace std;


class changfangti {    //输入长方体的长宽高,计算体积
public:
	int len;
	int wid;
	int high;
	void setcahng();
	void tiji();
};
void changfangti::setcahng() {
	cin >> len;
	cin >> wid;
	cin >> high;
}
void  changfangti::tiji() {
	int x;
	x = len * wid * high;
	cout << "长方体体积为:" << x << endl;
}
void main() {
	changfangti x,y,z;
	x.setcahng();
	x.tiji();
	y.setcahng();
	y.tiji();
	z.setcahng();  z.tiji();
}

3.商店销售某一商品,商店每天公布统一的折扣(discount)。同时允许销售人员在销售时灵活掌握售价

(price),在此基础上对一次购 10 件以上者,还可以享受 9.8 折优惠。现已知当天 3 个售货员销售情况为

售货员号(num) 售货件数(quantity) 售货单价(price)

101 5 23.5

102 12 24.56

103 100 21.5

请编程序,计算当日此商品的总销售款 sum 以及每件商品的平均售价。要求用静态数据成员和静态成员函

数。

提示:将折扣 discount,总销售款 sum 和商品销售总件数 n 声明为静态数据成员,再定义静态成员函

数 average(求平均售价)和 display(输出结果)。

#include<iostream>
using namespace std;

class store {
private:
	int num;
	static float sum;
	static float discount ;
	static int n;
	float price;
	int qualitity;
public:
	store(int n, int q, float p) : num(n), qualitity(q), price(p) {};
	void total();
	static float average();
	static void display();
};
void store::total() {
	float a=1.0 ;
	if (qualitity > 10) {
		a = 0.98;
	}
	sum += price * qualitity * a * (1 - discount);
	n += qualitity;
}
float store::average() {
	return(sum / n);
}
void store::display() {
	cout << "sum:" << sum << '\n' << store::average() << endl;
}
float store::discount = 0.05; //初始化静态对象的值
float store::sum = 0;
int store::n = 0;
void main() {
	store x[] = { store(101,23.5,5),store(102,24.56,12),store(103,21.5,100) };
	for (int i = 0; i < 3; i++) {
		x[i].total();
		store::display();
	}
}

静态数据成员和静态数据函数可以用来实现所有对象共有。也就是说各个对象中的数据成员是一样的,成员函数也是如此。

4.注意友元函数 Time::display 的作用。将程序中的 display 函数不放在 Time 类中,而

作为类外的普通函数, 然后分别在 Time 类和 Date 类中将 display 声明为友元函数。 在主函数中调用 display

函数, display 函数分别引用 Time 和 Date 两个类对象的私有数据,输出年、月、日和时、分、秒。

修改后上机调试和运行。

#include<iostream>
using namespace std;
class Date; //对Date类提前引用声明
class Time {
public:
	Time(int, int, int);
	friend void display(Time&,Date&);
	// friend void display(Date&); //display是成员函数,形参是Date类对象的引用
private:
	int hour;
	int minute;
	int sec;
};
class Date {
public:
	Date(int, int, int);
	friend void display(Time&, Date&);
private:
	int month;
	int day;
	int year;
};
Time::Time(int h, int m, int s) {
	hour = h;
	minute = m;
	sec = s;
}
void display(Time& d,Date&c) {
	cout << c.month<< "/" << c.day << "/" << c.year << endl;
	cout << d.hour << ":" << d.minute << ":" <<d. sec << endl;
}
Date::Date(int m, int d, int y) {
	month = m;
	day = d;
	year = y;
}
int main() {
	Time t1(10, 13, 56);
	Date d1(12, 25, 2004);
	display(t1,d1);
	return 0;
}

友元可以理解为”朋友”,这种类型可以访问私有(private)里的数据成员。

5.体会类模板的作用。

写为在类模板外定义各成员函数。

#include<iostream>
using namespace std;
template<class numtype>//类模板,声明一个类模板,虚拟类型名为numtype
class Compare { //类模板名为Compare
public:
	Compare(numtype a, numtype b) {
		x = a; y = b;
	}
	numtype max();
	
	numtype min() {
		return(x < y) ? x : y;  //类内定义
		}
private:
	numtype x, y;
};
template<class numtype>
numtype Compare<numtype>::max() {   //类外必须这么定义
	return(x > y) ? x : y;
}
int main() {
	Compare<int>cmp1(3, 7);  //定义对象cmp1,用于两个整数比较
	cout << cmp1.max() << "is the MAX of two integer numbers" << endl;
	cout << cmp1.min() << "is the MIN of two integer numbers." << endl<<endl;
	Compare<float>cmp2(45.78, 93.6);//定义对象cmp2,用于两浮点数比较
	cout << cmp2.max ()<< "is the MAX of two float numbers." << endl;
	cout << cmp2.min() << "is the MIN of two floatr numbers." << endl<<endl;
	Compare<char>cmp3('a', 'A');//定义对象cmp3,用于两个字符比较
	cout << cmp3.max() << "is the MAX of two characters ." << endl;
	cout << cmp3.min() << "is the MIN of two characters." << endl;
	return 0;
}

类模板适用于功能相同而数据类型不同的一些函数,正如上述程序。一样的功能但却用了int ,float,char三种不同的类型,且不用定义3中不同的类。要运用好numtype这个虚拟类型名。



版权声明:本文为m0_46217895原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。