实验3 this指针、常量和静态成员和友元函数的使用
1、 实验目的和要求:
(1) 掌握显式使用this指针的方法
(2) 掌握静态数据成员的意义及使用方法
(3) 掌握常量数据成员和常量成员函数的意义和使用方法
(4) 掌握友元函数和友元类的使用方法
2、 实验内容:
//代码较low,应付作业是够了,,,,,被嘲笑概不负责(笑哭)
(1)有如下类的定义。类成员函数copy用于实现两个对象的相互拷贝,请完成该函数的实现。(有两种方法即不用this 指针和用this指针)
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100000000
class Myclass
{
public:
Myclass(int a, int b) { x = a; y = b; }
void copy(Myclass & my);
void print()
{
cout << "x=" << x << endl;
cout << "y="<< y << endl;
}
private:
int x, y;
};
void main()
{
Myclass my(10, 20), t(30, 40);
my.print();
my.copy(t);
my.print();
}
void Myclass::copy(Myclass &my){
x = my.x;//this->x=my.x;
y = my.y;//this->y=my.y;
}
(2)设计一个类,实现两个复数的四则运算。实现加减乘除功能的函数用友元函数实现。
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100000000
void judge(double b){
if (b < 0){
cout << b << "i" << endl;
}
else if (b>0){
cout << "+" << b << "i" << endl;
}
}
class fushu{
private:
double a, bi;
public:
void set(double a, double b){
this->a = a;
this->bi = b;
}
friend class operation;
};
class operation{
public:
void add_show(fushu, fushu);
void div_show(fushu, fushu);
void mul_show(fushu, fushu);
void sub_show(fushu, fushu);
};
void operation::add_show(fushu s1, fushu s2){
double a = s1.a + s2.a;
double b = s1.bi + s2.bi;
cout << a;
judge(b);
}
void operation::sub_show(fushu s1, fushu s2){
s2.set(-s2.a, -s2.bi);
add_show(s1, s2);
}
void operation::mul_show(fushu s1, fushu s2){
double a = s1.a*s2.a - s1.bi*s2.bi;
double b = s1.a*s2.bi + s1.bi*s2.a;
cout << a;
judge(b);
}
void operation::div_show(fushu s1, fushu s2){
double mu = s2.a*s2.a + s2.bi*s2.bi;
s2.bi *= -1;
double a = s1.a*s2.a - s1.bi*s2.bi;
double b = s1.a*s2.bi + s1.bi*s2.a;
a /= mu;
b /= mu;
cout << a;
judge(b);
}
int main(){
fushu s1, s2;
s1.set(1, 2);
s2.set(3, 4);
operation ope;
ope.add_show(s1, s2);
ope.mul_show(s1, s2);
ope.sub_show(s1, s2);
ope.div_show(s1, s2);
}
(3)分析下面程序,给出横线部分的语句,写出程序的输出结果并分析m_count的功能。
#include <iostream>
using namespace std;
class A {
static int m_counter;
public:
A();
~A();
static void display();
}
int A::m_counter=0; //将m_counter初始化为0;
A::A()
{
m_counter++;
}
A::~A()
{
m_counter--;
}
void A::display()
{
cout<<”There are:”<<A::m_counter<<”objects of class A.\n”;
}
int main()
{
A a1;
A a2;
A a3;
A::display();
a1.display();
}
//结果:There are : 3objects of class A.
// There are : 3objects of class A.
(4)商店经销一种货物,货物成箱购进,成箱卖出,购进和卖出时以重量为单位,各箱的重量不一样,单价不一样,因此商店需要记录下目前库存的货物的总重量和总价值。编写一个程序,通过定义类Carlo来模拟商店货物购进和卖出的情况。
(本题目主要练习静态数据成员的使用,定义私有变量存每件货物的价格和重量,用静态数据成员存货物的总重量和总价钱,定义构造函数和析构函数,当定义新的对象完成初始化的功能和删除对象时,从总重量和总价钱中减去对象的重量和价格)
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<string.h>
#include<algorithm>
using namespace std;
class Carlo{
private:
double price, weight;
public:
static double Sweight, Sprice;
Carlo(double p, double w) :price(p), weight(w){
Sprice += price;
Sweight += weight;
}
~Carlo(){
Sprice -= price;
Sweight -= weight;
}
/* void buy(Carlo &s){
Sprice += s.price;
Sweight += s.weight;
}
void sell(Carlo &s){
Sprice -= s.price;
Sweight -= s.weight;
}*/
static void show(){
cout << "Sprice="<<Sprice << endl;
cout << "Sweight=" << Sweight << endl;
}
};
double Carlo::Sprice = 0;
double Carlo::Sweight = 0;
int main(){
Carlo m1(1, 10);
Carlo m2(2,3);
Carlo::show()
m2.~Carlo();
Carlo::show();
}
(5) 静态成员练习
1) 编写一个类Node,声明一个数据成员member和已经静态成员count,另构造函数初始化数据成员,并把静态数据成员加1,另析构函数把静态数据成员减1。
2) 在1)的基础上编写应用程序,创建3个对象,然后显示他们的数据成员和静态成员,再析构每个对象,并显示他们对静态数据成员的影响。
3) 修改2),让静态成员函数访问静态数据成员,并让静态数据成员是私有的。
#include<iostream>
#include<stdio.h>
#include<iomanip>
#include<string.h>
#include<algorithm>
using namespace std;
class node{
private:
int member;
static int count;
public:
node(int a){
count++;
member = a;
}
~node(){ count--; }
void show(){ cout << member << endl; }
static void show_count(){ cout << count << endl; }
};
int node::count = 0;
int main(){
node s1(1);
node s2(2);
node s3(3);
s1.show(); s2.show(); s3.show();
node::show_count();
s1.~node();
s2.~node();
s3.~node();
node::show_count();
}
(6)两个类分别为整型数集合类和实型数集合数类。将缺少的内容补齐。并完成要求的其它内容。
如:
class Intset
{ private:
int num[3];
public:
Intset ( int x,int y, int z)
{//添加初始化内容}
void print( )
{ //打印数据}
};
class floatset
{ private:
float num[3];
public:
floatset ( float x,float y, float z)
{
//添加初始化内容
}
void print( )
{ //打印数据
}
};
(1) 在Intset中再增加一个成员函数,将对象的整型数据拷贝的到floatset的对象中此成员函数的原型为:
void settofloat(floatset &set); //形参为拷贝的目标对象
(2)定义一个友元函数来实现上述的功能。
请分别完成两个程序。
(7) 分析以下程序的功能,把程序用三种方法(公有数据成员、友元、用成员函数访问私有数据成员)补充完整,实现对对象Animal的成员
(8) 设计一个整数链表类,满足栈操作。即,总在链表首插入结点,总在链表首取出(删除)结点。类中需有记录结点个数的数据成员。如果链表为空,而要做取出结点操作,则类必须给出错误信息。
编写应用程序,取100次随机数(范围10-200),每取到比前一个随机数大时,放入链表中,否则,略去。然后逐个取出,求其和。
用堆分配方法逐个产生满足条件的结点,插入链表中。每当从链表中取出一个结点时,要及时将结点删除。
求和工作不要在链表类中完成,以使该链表类具有通用性。
#include<iostream>
#include<time.h>
using namespace std;
class node{
public:
int x;
node *next;
friend class list;
};
class list{
private:
node *temp;
node *head;
node *tail;
public:
int len;
list();
void pop();
int front();
int tails();
bool empty();
void add(int x){ //添加函数
temp = new node; //开辟新节点
temp->x = x; //给节点赋值
temp->next = NULL; //给节点赋值
if (tail == NULL){ //当尾指针指向空时,说明链表为空
head = tail = temp; //头,尾指向temp这个临时节点
len++; //长度加一
}
else{
if (x>tails()){ //当数据大于上一个插入的数据
tail->next = temp; //尾的next指针指向新节点
tail = temp; //尾指向新节点
len++; //长度加一
}
}
}
};
int list::tails(){
if (tail)return tail->x;
}
list::list(){
head = tail = temp = NULL;
len = 0;
}
void list::pop(){
if (empty()){ //空链表无法弹出节点,返回错误信息
cout << "mistake" << endl;
}
else{
len--; //长度减一
node *t = head->next; //临时指针指向第二个节点
delete head; //清空头指针指向位置
head = t; //头指向第二个节点位置
}
}
int list::front(){
if (!empty())
return head->x;
else {
cout << "mistake" << endl;
return 0x3f3f3f3f;
}
}
bool list::empty(){
return len == 0;
}
int main(){
srand(time(0));
int n = 100;
list l;
int i = 8;
int num;
for (i = 0; i<n; i++)
{
num = rand() % 191 + 10;
l.add(num);
}
int sum = 0;
while (!l.empty()){
sum += l.front(); l.pop();
}
cout << sum;
}
(9) 设计合适的类结构,完成多项式加、减、乘运算。
3、思考题:
结合编程实践回答下列的问题。
(1) 什么是this指针,它的主要作用是什么?
(2) 总结静态成员的特点。
(3) 分别介绍const修饰数据成员、函数成员、对象的作用。
(4) 用合适的数据结构(给出类声明)描述下面的问题:
一个老师可以有多个助教,每个助教可以辅导多名学生。