#include <iostream>
using namespace std;
#include <string>
#include <cstring>
/*编写Person类,数据成员为姓名(20字符长度)、年龄(int)和性别(char)。
编写无参数的构造函数,其中姓名赋值为“XXX”,年龄0,性别m;
编写析构函数,在其中输出字符串“Now destroying the instance of Person”;
编写Register成员函数,为数据成员赋值;
编写showme成员函数,显示姓名、年龄和性别。
编写主函数:
用Person类创建2个指针,p1和 p2;
用new创建两个Person对象,分别将指针赋值给p1,p2;
用showme成员函数显示p1,p2所指对象的值;
再输入一组“姓名、年龄和性别”值,用成员函数Register为p1的成员赋值;
将p1所指对象的值赋值给p2所指对象;
用showme显示p1、p2所指对象的值。
删除动态对象。*/
/*
class Person
{
public:
Person(const char* name="XXX", int age=0, char sex='m');
~Person();
void Register(const char* name, int age, char sex);
void showme();
private:
char m_name[20];
int m_age;
char m_sex;
};
Person::~Person()
{
cout << "Now destroying the instance of Person" << endl;
}
Person::Person(const char* name, int age, char sex)
{
strcpy(m_name, name);
m_age = age;
m_sex = sex;
}
void Person::Register(const char* name, int age, char sex)
{
strcpy(m_name, name);
m_age = age;
m_sex = sex;
}
void Person::showme()
{
cout << m_name << " " << m_age << " " << m_sex << endl;
}
int main()
{
Person* p1 = new Person();
Person* p2 = new Person();
char name[20]={0};
int age = 0;
char sex;
cin >> name >> age >> sex;
cout << "person1:";
p1->showme();
cout << "person2:";
p2->showme();
p1->Register(name, age, sex);
*p2 = *p1;
cout << "person1:";
p1->showme();
cout << "person2:";
p2->showme();
delete p2;
p2=NULL;
delete p1;
p1=NULL;
//cout << "Hello world!" << endl;
return 0;
}*/
/*定义一个图书类Book,
数据成员包含name(书名)、author(作者)、price(定价),
为该类定义构造函数,
并提供各数据成员的getXXX()和setXXX()函数。
在main函数中创建图书对象数组,
将对应输入值依次输出。*/
/*
#include <iostream>
using namespace std;
#include <cstring>
class Book
{
public:
Book();
~Book();
Book(string name, string author, double price);
void setName(string name);
void setAuthor(string author);
void setPrice(double price);
string getName();
string getAuthor();
double getPrice();
void printInfo();
private:
string m_name;
string m_author;
double m_price;
};
void Book::setName(string name)
{
m_name = name;
}
void Book::setAuthor(string author)
{
m_author = author;
}
void Book::setPrice(double price)
{
m_price = price;
}
string Book::getName()
{
return m_name;
}
string Book::getAuthor()
{
return m_author;
}
double Book::getPrice()
{
return m_price;
}
Book::Book()
{
}
Book::~Book()
{
}
Book::Book(string name, string author, double price)
{
m_name = name;
m_author = author;
m_price = price;
}
void Book::printInfo()
{
cout << "name=" << getName()
<< ",author="<< getAuthor()
<< ",price=" << getPrice() << endl;
}
int main()
{
int icnt = 0;
cin >> icnt;
Book bookArray[icnt];
string name;
string author;
double price;
for(int i = 0; i < icnt; i++)
{
cin >> name >> author >> price;
bookArray[i] = Book(name, author, price);
}
for(int i = 0; i < icnt; i++)
{
bookArray[i].printInfo();
}
return 0;
}
*/
/*
定义一个交通工具(Vehicle)的类,
其中有数据成员:
速度(speed),体积(volume)。
其中有成员函数:
1、构造函数,通过创建带2个参数的构造函数,实现数据成员初始化功能。其中speed=循环次数,初始值为0;volume=循环次数+1.
2、析构函数
3、显示速度和体积值(show()),
4、设置速度(setSpeed(int ispeed)),
5、加速speedUp(int upspeed),实现功能为在原来速度的基础上+upspeed;
6、减速speedDown(int downspeed),实现功能为在原来速度的基础上-downspeed。
在main函数中的循环体内创建一个交通工具对象,该对象通过构造函数设置speed, volume的值。
其次,调用加速、减速的方法对速度进行改变,最终通过show()成员函数打印出来。
*/
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(int speed=0, int volume=1);
~Vehicle();
void show();
void speedUp(int upspeed);
void speedDown(int downspeed);
private:
int m_speed;
int m_volume;
};
Vehicle::Vehicle(int speed, int volume)
{
cout << "Vehicle is running!" << endl;
m_speed = speed;
m_volume = volume;
}
Vehicle::~Vehicle()
{
cout << "~Vehicle is running!" << endl;
}
void Vehicle::show()
{
cout << "speed = " << m_speed << " volume = " << m_volume << endl;
}
void Vehicle::speedUp(int upspeed)
{
m_speed+=upspeed;
}
void Vehicle::speedDown(int downspeed)
{
m_speed-=downspeed;
}
int main()
{
int icnt;
int upspeed;
int downspeed;
cin >> icnt;
cin >> upspeed;
cin >> downspeed;
for(int i = 0; i < icnt; i++)
{
Vehicle* pVeh = new Vehicle(i, i+1);
pVeh->speedUp(upspeed);
pVeh->speedDown(downspeed);
pVeh->show();
delete pVeh;
}
return 0;
}