第一章类与对象
1.声明一个名为Employee 的类,它包含如下数据成员:age、yearOfService
和Salary。
2.重写Employee 类的生命,使其数据成员为私有、并提供获得和设置每个
数据成员的公有存取器方法。
3.编写一个程序,使用Employee 类来创建两个Employee 的对象,设置它
们的age、yearOfService 和salary,并打印它们的值。您还需要添加存取器方法
的代码。
4.继续练习3,给Employee 类编写一个方法,它报告雇员挣多少千美金(四
舍五入到最接近的千数)。
5.修改Employee 类,以便能够在创建Employee 对象时初始化age 、
yearOfService 和salary。
6.查错:下面声明有什么错误?
class Square {
public:
int side;
}
7.查错:为什么下面的类声明不是特别有用?
class Cat {
int getAge() const;
private:
int itsAge();
};
8.查错:编译器将在下面的代码中找到哪3 处错误?
class TV {
public:
void setStation(int Station);
int getStation() const;
private:
int itsStation;
};
Main() {
TV myTV;
my.TV.itsStation = 9;
TV.setStation(10);
TV.myOtherTv(2);
6. 类声明的后面缺少封号
7. getAge()方法没有说明是public还是private,默认为private,因此无法被外部调用。
8. ① Main()
② 没有成员函数的定义。
③ 没有带一个参数的构造函数
1.声明一个名为Employee 的类,它包含如下数据成员:age、yearOfService
和Salary。
2.重写Employee 类的生命,使其数据成员为私有、并提供获得和设置每个
数据成员的公有存取器方法。
3.编写一个程序,使用Employee 类来创建两个Employee 的对象,设置它
们的age、yearOfService 和salary,并打印它们的值。您还需要添加存取器方法
的代码。
4.继续练习3,给Employee 类编写一个方法,它报告雇员挣多少千美金(四
舍五入到最接近的千数)。
5.修改Employee 类,以便能够在创建Employee 对象时初始化age 、
yearOfService 和salary。
6.查错:下面声明有什么错误?
class Square {
public:
int side;
}
7.查错:为什么下面的类声明不是特别有用?
class Cat {
int getAge() const;
private:
int itsAge();
};
8.查错:编译器将在下面的代码中找到哪3 处错误?
class TV {
public:
void setStation(int Station);
int getStation() const;
private:
int itsStation;
};
Main() {
TV myTV;
my.TV.itsStation = 9;
TV.setStation(10);
TV.myOtherTv(2);
}
//第一题
class Employee
{
int age;
int yearOfService;
double salary;
};
//第二题
class Employee
{
public:
void setAge(int age);
int getAge();
void setYearOfService(int yearOfService);
int getYearOfService();
void setSalary(double salary);
double getSalary();
private:
int age;
int yearOfService;
double salary;
};
//第三题
#include <iostream>
using namespace std;
class Employee {
public:
void setAge(int age);
int getAge();
void setYearOfService(int yearOfService);
int getYearOfService();
void setSalary(double salary);
double getSalary();
private:
int age;
int yearOfService;
double salary;
};
void Employee::setAge(int age) {
this->age = age;
}
int Employee::getAge() {
return this->age;
}
void Employee::setYearOfService(int yearOfService) {
this->yearOfService = yearOfService;
}
int Employee::getYearOfService() {
return this->yearOfService;
}
void Employee::setSalary(double salary) {
this->salary = salary;
}
double Employee::getSalary() {
return this->salary;
}
int main() {
Employee xiaoming;
xiaoming.setAge(3);
xiaoming.setYearOfService(1);
xiaoming.setSalary(10000.00);
cout << "xiaoming" << endl;
cout << "age: " << xiaoming.getAge() << endl;
cout << "year of service: " << xiaoming.getYearOfService() << endl;
cout << "salary: " << xiaoming.getSalary() << endl;
cout << endl;
Employee wangnima;
wangnima.setAge(4);
wangnima.setYearOfService(2);
wangnima.setSalary(12500.00);
cout << "wangnima" << endl;
cout << "age: " << wangnima.getAge() << endl;
cout << "year of service: " << wangnima.getYearOfService() << endl;
cout << "salary: " << wangnima.getSalary() << endl;
return 0;
}
//第四题
#include <iostream>
using namespace std;
class Employee {
public:
void setAge(int age);
int getAge();
void setYearOfService(int yearOfService);
int getYearOfService();
void setSalary(double salary);
double getSalary();
int getSalaryInThousands();
private:
int age;
int yearOfService;
double salary;
};
void Employee::setAge(int age) {
this->age = age;
}
int Employee::getAge() {
return this->age;
}
void Employee::setYearOfService(int yearOfService) {
this->yearOfService = yearOfService;
}
int Employee::getYearOfService() {
return this->yearOfService;
}
void Employee::setSalary(double salary) {
this->salary = salary;
}
double Employee::getSalary() {
return this->salary;
}
int Employee::getSalaryInThousands() {
return (this->salary + 500) / 1000;
}
int main() {
Employee xiaoming;
xiaoming.setAge(3);
xiaoming.setYearOfService(1);
xiaoming.setSalary(10000.00);
cout << "xiaoming" << endl;
cout << "age: " << xiaoming.getAge() << endl;
cout << "year of service: " << xiaoming.getYearOfService() << endl;
cout << "salary: " << xiaoming.getSalaryInThousands() << endl;
cout << endl;
Employee wangnima;
wangnima.setAge(4);
wangnima.setYearOfService(2);
wangnima.setSalary(12500.00);
cout << "wangnima" << endl;
cout << "age: " << wangnima.getAge() << endl;
cout << "year of service: " << wangnima.getYearOfService() << endl;
cout << "salary: " << wangnima.getSalaryInThousands() << endl;
return 0;
}
//第五题
#include <iostream>
using namespace std;
class Employee {
public:
Employee(int age, int yearOfService, double salary) : age(age), yearOfService(yearOfService), salary(salary) { }
void setAge(int age);
int getAge();
void setYearOfService(int yearOfService);
int getYearOfService();
void setSalary(double salary);
double getSalary();
int getSalaryInThousands();
private:
int age;
int yearOfService;
double salary;
};
void Employee::setAge(int age) {
this->age = age;
}
int Employee::getAge() {
return this->age;
}
void Employee::setYearOfService(int yearOfService) {
this->yearOfService = yearOfService;
}
int Employee::getYearOfService() {
return this->yearOfService;
}
void Employee::setSalary(double salary) {
this->salary = salary;
}
double Employee::getSalary() {
return this->salary;
}
int Employee::getSalaryInThousands() {
return (this->salary + 500) / 1000;
}
int main() {
Employee xiaoming(3, 1, 10000.00);
cout << "xiaoming" << endl;
cout << "age: " << xiaoming.getAge() << endl;
cout << "year of service: " << xiaoming.getYearOfService() << endl;
cout << "salary: " << xiaoming.getSalaryInThousands() << endl;
cout << endl;
Employee wangnima(4, 2, 12500.00);
cout << "wangnima" << endl;
cout << "age: " << wangnima.getAge() << endl;
cout << "year of service: " << wangnima.getYearOfService() << endl;
cout << "salary: " << wangnima.getSalaryInThousands() << endl;
return 0;
}
6. 类声明的后面缺少封号
7. getAge()方法没有说明是public还是private,默认为private,因此无法被外部调用。
8. ① Main()
② 没有成员函数的定义。
③ 没有带一个参数的构造函数