C++ primer plus第十章课后习题--对象和类

对象和类

    1. 什么是类?

      类是一种将抽象转换为用户定义类型的C++工具,相当于是一种复合的结构,其中定义了基本类型和方法;

      2 . 类如何实现抽象、封装和数据隐藏?

      类设计将公有接口和实现细节分开,公有接口表示设计的抽象组件,将实现的细节放在一起并将它们与抽象分开被称为封装, 防止被外部程序直接访问数据为数据隐藏,一般定义在private中,数据隐藏也是一种封装,将实现的细节隐藏在私有部分中。

      3 . 对象和类之间的关系是什么?

      类可以创建对象,对象拥有类中所有的基本类型和方法,而且同一个类创建的多个对象的基本类型是对类的基本类型的一份拷贝,共用同一套方法。

      4.除了是函数之外,类函数成员与类数据成员之间的区别是什么?

      类函数成员在类外进行定义时,需要作用域解析运算符(::)来标识函数所属的类;

      类函数可以对类内的私有数据进行访问修改;

      5 .类构造函数在何时被调用?类析构函数呢?

      在创建一个对象时,默认调用无参构造函数,对象过期时,程序将自动调用析构函数,如果构造函数使用new来分配内存,则析构函数将使用delete来释放这些内存,临时对象创建之后,也会调用析构函数进行销毁。

      1. this和*this是什么?

        this指针指向用来调用成员函数的对象,*this可以表示调用对象的别名,相当于调用对象本身。

    1. 下面是一个非常简单的类定义:

      class Person{
      	private:
          	static const int LIMIT=25;
          	string lname;   //Person's last name
          	char fname[LIMIT]; //Person's first name
          public:
          	Person()
              {
                  lname="";
                  fname[0]='\0';
              }
          	Person(const string & ln,const char * fn="Heyyou");
          	void Show() const;
          	void FormalShow() const;
      };
      

      它使用了一个string对象和一个字符数组,让您能够比较它们的用法。请根据未定义的方法的代码,以完成这个类的实现。再编写一个使用这个类的程序,它使用了三种可能的构造函数调用(没有参数、一个参数和两个参数)以及两种显示方法。下面是一个使用这些构造函数和方法的例子:

      Person one;
      Person two("Smythecraft");
      Person three("Dimwiddy","Sam");
      one.Show();
      cout<<endl;
      one.FormalShow();
      
      #include<iostream>
      #include<string>
      #include<cstring>
      
      using namespace std;
      
      class Person{
      	private:
          	static const int LIMIT=25;
          	string lname;   //Person's last name
          	char fname[LIMIT]; //Person's first name
          public:
          	Person()
              {
                  lname="";
                  fname[0]='\0';
              }
          	Person(const string & ln,const char * fn="Heyyou");
          	void Show() const;
          	void FormalShow() const;
      };
      
      Person::Person(const string & ln,const char * fn)
      {
      	lname=ln;
      	strcpy(fname,fn);   //这里要使用strcpy()拷贝函数,不能使用fname=fn; 否则报错 [Error] incompatible types                         //                           in assignment of 'const char*' to 'char [25]' 
      						//这里不能把一个指向字符串的指针赋值给数组,就算数组名指向数组的首地址,两者本质上是不一样                           //的,指针是一个变量,而数组名是一个常量,string类型就可以
      						//用等于号直接实现拷贝; 
      	 //this->lname=ln;
      	//strcpy(this->fname,fn);
      }
      void Person::Show() const
      {
      	cout<<"Format: "<<fname<<"(first name)"<<" "<<lname<<"(last name)."<<endl;
      }
      void Person::FormalShow() const
      {
      	cout<<"Format: "<<lname<<"(last name)"<<" "<<fname<<"(first name)."<<endl;
      }
      
      int main()
      {
      	Person one;
      	Person two("Smythecraft");
      	Person three("Dimwiddy","Sam");
      	one.Show();
      	cout<<endl;
      	one.FormalShow();
      	
      	return 0;
      }
      

      上面是一个简单的实现。

    2. 考虑下面的结构声明:

    struct customer
    {
      	char fullname[35];
        double payment;
    };
    

    编写一个程序,它从栈中添加和删除customer结构(栈用Stack类声明)。每次customer结构被删除时,其payment的值都被加入到总数中,并报告总数。注意:应该可以直接使用Stack类而不做修改;只需修改typedef声明,使得Item的类型为customer,而不是unsigned long即可。

#include<iostream>
#include<string> 
#include<cctype>
using namespace std;
struct customer
{
  	char fullname[35];
    double payment;
};

typedef struct customer  Item;
class Stack{
	private:
		enum {MAX=10};
		Item items[MAX];

		int top;
	public:
		Stack();
		bool isempty() const;
		bool isfull() const;
		bool push(const Item & item);
		bool pop(Item & item);
	
};
Stack::Stack()
{

	top=0;
}
bool Stack::isempty() const
{
	return top==0;
}
bool Stack::isfull() const
{
	return top==MAX;
	
}
bool Stack::push(const Item & item)
{
	if(top<MAX)
	{
		items[top++]=item;
		return true;
	}
	
	else
	{
		return false;
	}
}
bool  Stack::pop(Item & item)
{
	if(top>0)
	{
		item=items[--top];
		return true;
	}
	else
	{
		return false;
	}
}

int main()
{
	char ch;
	Stack st;
	Item temp;
	double total=0.0;
	
	cout<<"a to add a customer"<<endl;
	cout<<"d to pop a customer"<<endl;
	cout<<"q to exit the menu"<<endl;
	cout<<"Please enter your choice: ";
	
	while(cin>>ch && tolower(ch)!='q')
	{
		system("cls");
		while(cin.get()!='\n')
		{
			continue;
		}
		if(tolower(ch)!='a'&&tolower(ch)!='d')
		{
			cout<<"Please enter a,d or q: ";
		}
		switch(tolower(ch))
		{
			case 'a':
				{
					cout<<"Your are adding a customer!"<<endl; 
					cout<<"Enter the customer's fullname: ";
					cin.getline(temp.fullname,35);
					cout<<"Enter the customer's payment: ";
					while(!(cin>>temp.payment))
					{
						cin.clear();
						while(cin.get()!='\n')
						{
							continue;
						}
						cout<<"Please enter an number: ";
					}
					if(st.isfull())
					{
						cout<<"Stack is full. Can't add new customer!"<<endl;
					}
					else
					{
						st.push(temp);
						
					}
					break;
				}
			case 'd':
				if(st.isempty())
				{
					cout<<"Stack is empty. No any customer"<<endl;
				}
				else
				{
					st.pop(temp);
					total+=temp.payment;
					cout<<"Customer "<<temp.fullname<<" will quit!"<<endl;
					cout<<"Now the total payment are $"<<total<<endl;
				}
				break;
		}
		cout<<"\n\n\n";
		cout<<"a to add a customer"<<endl;
		cout<<"d to pop a customer"<<endl;
		cout<<"q to exit the menu"<<endl;
		cout<<"Please enter your choice: ";
	}
	cout<<"Bye"<<endl;

	return 0;
}
  1. 下面是一个类声明:

    class Move
    {
      	private:
        	double x;
        	double y;
       	public:
        	Move(double a=0,double b=0); //sets x,y to a,b
        	void showmove() const;
        	Move add(const Move & m) const;
        	void reset(double a=0,double b=0);
    };
    

    简单实现:

    #include<iostream>
    using namespace std;
    
    class Move
    {
      	private:
        	double x;
        	double y;
       	public:
        	Move(double a=0,double b=0); //sets x,y to a,b
        	void showmove() const;
        	Move add(const Move & m) const;
        	void reset(double a=0,double b=0);
    };
    
    Move::Move(double a,double b)
    {
    	x=a;
    	y=b;
    }
    void Move::showmove() const
    {
    	cout<<"Showing current x,y values:"
    	<<" x = "<<x<<" y = "<<y<<endl;
    }
    Move Move::add(const Move & m) const
    {
    	return Move(x+m.x,y+m.y);
    }
    void Move::reset(double a,double b) //类中已经对a,b变量进行了定义,在全局域中进行实现时(实现这个词感觉有失偏颇),
        								//这里就不需要再进行定义了,上面的构造函数也是一样。
    {
    	x=a;
    	y=b;
    }
    int main()
    {
    	Move one(1.0,2.0);
    	one.showmove();
    	Move two;
    	two.reset(3.0,4.0);
    	two.showmove();
    	one=one.add(two);
    	one.showmove();
    	
    	
    	
    	return 0;
    }
    
    • 这里只写了编程练习中的三题,其中第二题的设计和思路借鉴了网上一位大佬的文章,因为没找着,就不贴上去了
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

TIEZ@han

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

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

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

打赏作者

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

抵扣说明:

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

余额充值