类和对象
有如下程序,下列叙述中正确的是()。
#include<iostream> using namespace std;
class Instrument{ public: virtual void Display()=0; };
class Piano : public Instrument{ public: void Display(){/*函数体程序略*/} };
int main(){ Instrument s; Instrument *p=0; //... return 0; }有如下程序,若程序运行时输出结果是“文学”,则划线处缺失的语句是()。
#include
using namespace std;
class Book{
public:
Book(char*t="") {strcpy(title,t);}
private:
char title[40];
};
class Novel: public Book{
public:
Novel(chart=""): Book(t){}
charCategory() const { return "文学";}
};
int main(){
Book pb;
pb=new Novel();
cout<Category();
return 0;
}指出下列程序片段中的错误标号,写出正确语句或解释错在何处。
1)
①int index=675; ②const int *ptr=&index; ③int *const ntptr=&index; ④*ptr=555; ⑤*ntptr=666; ⑥int another=8; ⑦ptr=&another; ⑧ntptr=&another;
2)
①int arrp; ②arrp=new int[15]; ③delete arrp;
下面程序为什么会编译错误,并改正错误(提出解决办法)。
class window
{
protected:
int basedata;
};
class border: public window
{ };
class menu: public window
{ };
class border_and_menu: public border,public menu
{
public:int show()
{
return basedata;
}改正下面程序段中的错误,写出整个正确的程序段
template<T>
void print(T *a)
{ cout<<a<<’\n’;}
void main( )
{
const int x=0;
cout<<y<<’\n’;
int y;
x=5;
int* p
p=&y;
print(p);
return 0;
}写出下面程序的执行结果:
1)
#include <iostream>
using namespace std;
class A
{
friend double count(A&);
public:
A(double t, double r):total(t),rate(r){}
private:
double total;
double rate;
};
double count(A& a)
{
a. total+=a.rate*a.total;
return a.total;
}
int main(void)
{
A x(80,0.5),y(100,0.2);
cout<<count(x)<<','<<count(y)<<'\n';
cout<<count(x)<<'\n';return 0;
}2)
#include<iostream>
using namespace std;
class Count
{
private:
static int counter;
int obj_id;
public:
Count(); //constructor
static void display_total(); //static function
void display();
~Count(); //destructor
};
int Count::counter; //definition of static data member
Count::Count() //constructor
{
counter++;
obj_id = counter;
}
Count::~Count() //destructor
{
counter--;
cout<<"Object number "<<obj_id<<" being destroyed\n";
}
void Count::display_total() //static function
{
cout <<"Number of objects created is = "<<counter<<endl;
}
void Count::display()
{
cout << "Object ID is "<<obj_id<<endl;
}
int main(void)
{
Count a1;
Count::display_total();
Count a2, a3,a4;
Count::display_total();
a2.display();
a4.display();
return 0;
}3)
#include <iostream >
using namespace std;
class BASE
{
char c;
public:
BASE(char n):c(n){}
virtual ~BASE(){cout<<c;}
};
class DERIVED:public BASE
{
char c;
public:
DERIVED(char n):BASE(n+1),c(n){}
~DERIVED(){cout<<c;}
};
int main(void)
{
DERIVED('X');
return 0;
}程序填空:
#include <iostream>
using namespace std;
class A
{
______(1)______
char name[80];
public:
A( ____(2)______ ) {____(3)______ }
};
class B_____(4)_______
{
public:
B(const char*n)_____(5)_______{}
void PrintName( ) {cout<<”name:”<<name<<endl;};
};
void main( )
{
B b1(“Ling Li”);
b1.PrintName( ) ;
}//执行结果:name: Ling Li编写程序:定义抽象基类Shape,由它派生出五个派生类:Circle(圆形)、Square(正方形)、Rectangle(长方形)、Trapezoid(梯形)和Triangle(三角形),用虚函数分别计算各种图形的面积,并求出它们的和。要求用基类指针数组。使它的每一个元素指向一个派生类的对象。
注:主函数中定义如下对象
Circle circle(12.6);
Square square(3.5);
Rectangle rectangle(4.5,8.4);
Trapezoid trapezoid(2.0,4.5,3.2);
Triangle triangle(4.5,8.4);
请在下面程序的横线处填上适当内容,以使程序完整,并使运行结果为:2/10/2004
#include <iostream.h>
static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31};
class date
{int mo,da,yr;
public:
date(int m,int d,int y){mo=m;da=d;yr=y;}
date(){}
void disp(){cout<<mo<<"/"<<da<<"/"<<yr<<endl;}
void leap(int a){
if(a%4==0&&a%100!=0||a%400==0)
dys[1]=29;
else dys[1]=28;}
friend date operator+(_______(1)_______)
{day+=d.da;
d.leap(d.yr);
while(_______(2)_______)
{d.leap(d.yr);
day-=dys[d.mo-1];
if(++d.mo==13)
{d.mo=1;
d.yr++;}
}
d.da=day;
return d;
}
};
int main()
{date d1(2,10,2003),d2;
int n;
cin>>n; //从键盘输入365
while(n<0){cin>>n;}
d2=d1+n;
d2.disp();
}请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:n=30
#include <iostream.h>
template <class T>
class Sample
{T n;
public:
Sample(){}
Sample(T i){n=i;}
Sample<T> operator+(const Sample<T> &);
void disp(){cout<<"n="<<n<<endl;}
};
template <class T>
Sample<T> Sample<T>::operator+(const Sample<T> &s){Sample<T> temp;
_______(1)_______;
return temp;
}
int main()
{Sample<int> s1(10),s2(20),s3;
s3=s1+s2;
_______(2)_______;
}请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为
A Constructor2
B Constructor2
x1=12
x2=2
#include <iostream.h>
class A
{public:
A(){cout<<"A Constructor1"<<endl;}
A(int i){x1=i;cout<<"A Constructor2"<<endl;};
void dispa(){cout<<"x1="<<x1<<endl;}
private:
int x1;
};
class B:public A
{
public:
B(){cout<<"B Constructor1"<<endl;}
B(int i): _____(1)_____{x2=i;cout<<"B Constructor2"<<endl;}
void dispb()
{_____(2)_____;
cout<<"x2="<<x2<<endl;
}
private:
int x2;
};
int main()
{B b(2);
b.dispb();
}请在下面程序的横线处填上适当字句,以使程序完整,并使程序的输出为
构造 base 对象
调用 base::f()
构造 derive 对象
调用 derive::f()
#include <iostream.h>
class base
{
public:
base()
{cout<<"构造base对象"<<endl;
f();
}
_________(1)_________{cout<<"调用base::f()"<<endl;}
};
class derive:public base
{
public:
derive()
{cout<<"构造derive对象"<<endl;
_________(2)_________;
}
void f(){cout<<"调用derive::f()"<<endl;}
};
int main()
{
derive d;
}下面程序用STL的条件计数算法和自定义的函数对象对一个存放在整数向量类对象中的学生成绩进行统计及格人数并显示结果。请在下面程序的横线处填上适当字句,以使程序完整。
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
class Pass
{
public:
____________________(1)___________________
{
return x>=60;
}
};
int main()
{
vector<int> a;
}
void disp(){cout<<"n="<<n<<endl;}
};
intmain()
{Sample s(10);
s--;
s.disp();
}
int count;
a.push_back(78);
a.push_back(92);
a.push_back(52);
count = ____________________(2)___________________;
cout<<"count="<<count<<endl;
}读程序写结果
1.
#include <iostream.h>
class CSample
{
private:
int n;
static int k;
public:
CSample(int i){n=i;k++;}
void disp();
};
void CSample::disp()
{cout<<"n="<<n<<",k="<<k<<endl;}
int CSample::k=0;
int main()
{CSample a(10),b(20),c(30);
a.disp();
b.disp();
c.disp();
}2.
#include <iostream.h>
class Sample
{int n;
public:
Sample(){}
Sample(int m){n=m;
}
Sample operator--(int)
{Sample old=*this;
n--;
return old;
}
void disp(){cout<<"n="<<n<<endl;}
};
int main()
{Sample s(10);
s--;
s.disp();
}3.
#include <iostream.h>
#include <string.h>
template <class T>
T max(T x, T y)
{
return (x > y ? x : y);
}
char *max(char *x, char *y)
{
if (strcmp(x, y) >= 0)
return x;
else
return y;
}
int main()
{
cout << max(2, 5) << endl;
cout << max(3.14,4.12) << endl;
cout << max("China", "Beijing") << endl;
}4.
#include <stack>
#include <iostream>
#include <string>
using namespace std;
int main() {
stack<char> s;
string str;
cin >> str;//从键盘输入字符串LoveChina
for(string::iterator iter = str.begin(); iter != str.end(); ++iter)
s.push(*iter);
while (!s.empty()) {
cout << s.top();
s.pop();
}
cout << endl;
}5.
#include <iostream>
using namespace std;
int divide(int x, int y)
{
if (y == 0)
throw x;
return x / y;
}
int main()
{try {
cout << "8/3 = " << divide(8, 3) << endl;
cout << "6/0 = " << divide(6, 0) << endl;
cout << "9/2 = " << divide(9, 2) << endl;
} catch (int e) {
cout << e << " is divided by zero!" << endl;
}
cout << "That is ok." << endl;
}编程实现小型公司的工资管理。该公司主要有4类人员:经理(manager)、兼职技术人员(technician)、销售员(salesman)和销售经理(salesmanager)。要求存储这些人员的编号、姓名和月工资,计算月工资并显示全部信息。月工资计算办法是:经理拿固定月薪8000元,兼职技术人员按每小时100元领取月薪,销售员按当月销售额的4%提成,销售经理既拿固定月工资也领取销售提成,固定月工资为5000元,销售提成为所管辖部门当月销售总额的千分之五。兼职技术人员一个月工作小时数、销售员一个月销售额、销售经理所管辖部门一个月销售总额由各个类的成员函数完成设置。(要求用抽象类和类继承)
#include <iostream>
using namespace std;
//雇员类
class Employee {
protected :
int _id;
double _salary;
string _name;
public:
Employee(int id,string name)
:_id(id),_name(name)
{}
virtual ~Employee(){}
virtual double CalcSalary()=0;
};
//经理类
class Manager:public Employee{
public:
Manager(int id,string name):Employee(id,name){}
virtual double CalcSalary(){
_salary=8000;
return _salary;
};
};
//兼职技术员类
class Technician:public Employee{
int _workHour;
public:
Technician(int id,string name):Employee(id,name){}
void SetWorkHour(int workHour){
_workHour = workHour;
}
virtual double CalcSalary(){
_salary=100*_workHour;
return _salary;
};
};
//销售经理类
class Salesman:public Employee{
double _salesMount;
public :
Salesman (int id,string name):Employee(id,name){}
void SetSalesMount(double salesMount){
_salesMount=salesMount;
}
virtual double CalcSalary(){
_salary=0.04*_salesMount;
return _salary;
};
};
class SalesManager:public Employee{
double _salesTotalMount;
public:
SalesManager (int id,string name):Employee(id,name){}
void
SetSalesTotalMount(double salesTotalMount){
_salesTotalMount=salesTotalMount;
}
virtual double CalcSalary(){
_salary =5000 +0.005 * _salesTotalMount;
return _salary;
};
};
int main()
{
Manager zhang(1000, "张三");
Technician li(1000, "李四");
Salesman wang(1000, "王五");
SalesManager zhao(1000, "赵六");
li.SetWorkHour(50);
wang.SetSalesMount(175000);
zhao.SetSalesTotalMount(1500000);
Employee *pEmployeeA[]={&zhang,&li,&wang,&zhao};
for (int i = 0; i <4; i++) {
cout << pEmployeeA [i] ->CalcSalary() << endl;
}
}定义一个矩阵类(Matrix),设计相关的构造函数、析构函数等,采用运算符重载方式实现矩阵的加、减运算。编写main函数,对以上所有功能进行测试。注意:按照实际情况大小使用内存空间,矩阵的加、减运算不要造成内存浪费。
#include <iostream>
#include <vector>
#include <cstdlib>
using
namespace std;
class
Matrix {
public:
Matrix (int row, int col); //构造row x col大小矩阵
Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加
Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减
void Display () const;//显示矩阵
//矩阵元素,可作为右值
const int &at (int row, int col) const {
return _datas [row][col];
}
//矩阵元素,可作为左值
int& at (int row, int col) {
return _datas [row][col];
}
//矩阵行数
int GetRowCount () const {
return _datas.size ();
}
//矩阵列数
int GetColCount () const {
return _datas [0].size ();
}
private:
//存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值
vector<vector<int> > _datas;
};
Matrix::Matrix (int row, int col)
{
for (int i = 0; i < row; i++) {
vector<int> dataRow (col);
_datas.push_back (dataRow);
}
}
Matrix Matrix::operator + (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) + rhs.at (i, j);
}
}
return result;
}
Matrix Matrix::operator - (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) - rhs.at (i, j);
}
}
return result;
}
void Matrix::Display () const
{
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
cout.width (5);
cout << at (i,j);
}
cout << endl;
}
cout << endl;
}
int main()
{
Matrix A (5, 6), B (5, 6);
int i, j;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
A.at(i, j) = rand () % 100 ;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
B.at(i, j ) = rand () % 100;
A.Display();
B.Display();
Matrix C = A + B;
C.Display();
Matrix D = A - B;
D.Display();
return 0;
}#include <iostream>
#include <vector>
#include <cstdlib>
using
namespace std;
class
Matrix {
public:
Matrix (int row, int col); //构造row x col大小矩阵
Matrix operator + (const Matrix &rhs) const;//相同大小矩阵相加
Matrix operator - (const Matrix &rhs) const;//相同大小矩阵相减
void Display () const;//显示矩阵
//矩阵元素,可作为右值
const int &at (int row, int col) const {
return _datas [row][col];
}
//矩阵元素,可作为左值
int& at (int row, int col) {
return _datas [row][col];
}
//矩阵行数
int GetRowCount () const {
return _datas.size ();
}
//矩阵列数
int GetColCount () const {
return _datas [0].size ();
}
private:
//存放矩阵元素,本类没有直接动态分配,无需重载拷贝构造和赋值
vector<vector<int> > _datas;
};
Matrix::Matrix (int row, int col)
{
for (int i = 0; i < row; i++) {
vector<int> dataRow (col);
_datas.push_back (dataRow);
}
}
Matrix Matrix::operator + (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) + rhs.at (i, j);
}
}
return result;
}
Matrix Matrix::operator - (const Matrix &rhs) const
{
Matrix result (this->GetRowCount(), this->GetColCount());
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
result.at(i,j) = this->at(i, j) - rhs.at (i, j);
}
}
return result;
}
void Matrix::Display () const
{
for (unsigned int i = 0; i < _datas.size (); i++) {
for (unsigned int j = 0; j < _datas [i].size (); j++) {
cout.width (5);
cout << at (i,j);
}
cout << endl;
}
cout << endl;
}
int main()
{
Matrix A (5, 6), B (5, 6);
int i, j;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
A.at(i, j) = rand () % 100 ;
for (i = 0; i < A.GetRowCount(); i++)
for (j = 0; j < A.GetColCount(); j++)
B.at(i, j ) = rand () % 100;
A.Display();
B.Display();
Matrix C = A + B;
C.Display();
Matrix D = A - B;
D.Display();
return 0;
}请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:
2,1
4,3
#include<iostream.h>
class A
{
int a;
public:
A(int i=0){a=i;}
Int Geta(){return a;}
};
class B
{
A a;
int b;
public:
B(int i=0,int j=0):___________(1)____________{}
void display(){cout<<a.Geta()<<’,’<<b<<endl;}
};
void main()
{
B b[2]={B(1,2),B(3,4)};
for(int i=0;i<2;i++)
_____________(2)______________;
}下面程序中A是抽象类。请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:
B1 called
B2 called
#include<iostream.h>
class A
{
public:
______________(1)____________;
};
class B1:public A
{
public:
void display(){cout<”B1 called”<<endl;
};
class B2:public A
{
public:
void display(){cout<<”B2 called”<<endl;
};
void show(_________(2)________)
{
p->display();
}
void main()
{
B1 b1;
B2 b2;
A* p[2]={&b1,&b2};
for(int i=0;i<2;i++)
show(p[i]);
}请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为:
Name:王小明
Grade:90
#include<iostream.h>
#include<string.h>
class Person
{
char name[20]
public:
Person(char* s){strcpy(name,s);}
void display(){cout<<”Name:”<<name<<endl;}
};
class Student:public Person
{
int grade;
public:
Student(char* s,int g):________(1)_____{grade=g;}
void display(){
___________(2)___________;
cout<<”Grade:”<<grade<<endl;
}
};
void main()
{
Student s(“王小明”,90);
s.display();
}请在下面程序的横线处填上适当内容,以使程序完整,并使程序的输出为5。
#include<iostream.h>
class Integer
{
int x;
public:
Integer(int a=0){x=a;}
void display(){cout<<x<<endl;}
_____________(1)_____________;
};
Integer Max(Integer a,Integer b)
{
if(__________(2)_________)
return a;
return b;
}
void main()
{
Integer a(3),b(5),c;
c=Max(a,b);
c.display();
}