c++大题题库

这是给同学们整理的题库,不全(请同学们见谅)

不保证会不会出现原题,但是尽可能的帮大家把以前出现过的题,给大家整理出来了

希望大家都能过,并且取得高分

有的可能需要改一下数

                                                                                                                                 ----sxybin

写一下步骤吧:

首先使用 Ctrl + f 

然后 右上角会弹出搜索框

然后搜索题目的一段文字(可以从e2e平台上复制一小段话,粘贴到搜索框中)例如下:

最后点回车(可能一次不行,那就点两下)

因为csdn有复制限制,必须登录才能复制,所以给同学们总结了两个方法

方法1:

直接登录csdn账号进行复制

方法2:

1.复制本文章链接,用edge浏览器打开

 

2.edge右上角三个点,找到拓展

3.打开 Microsoft Edge加载项

 4.搜索 Tampermonkey,获取

 如果以前下载过就点击右上角有个小拼图

找到它,鼠标左键单击

 

 然后点击获取新脚本,就会出现下一步

5.搜索CSDN

 6.打开第一个安装

安装此脚本后,它会自动弹出下面的界面,然后继续点击安装

 7.然后刷新界面就可以免登录复制

没登陆账号之前

登陆账号之后

安装脚本之后

一、运算符操作

1.某星球上有名为Jupyter的类,包含两个doulbe类型的私有变量xv与yv
若Jupyter对象的私有变量为2.2和3.3,输出的形式为: 2.2x+3.3y
两个Jupyter对象j1与j2相加的规律为: (a1x+b1y)+(a2x+b2y)=(a1+a2)x+(b1+b2)y
j1 += j2; 与j1 = j1+j2; 等价
请补充程序完成如下功能:
例:
(1)输入:1.1 2.2 3.3 4.4 输出: 4.4x+6.6y
(2)输入:1.1 2.2 3.3 -4.4 输出:4.4x-2.2y
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
    double x1,y1,x2,y2;
    cin>>x1>>y1>>x2>>y2;

    Jupyter j1(x1,y1),j2(x2,y2);
    j1 +=j2;
    j1.show();
    return 0;

代码如下:

#include <iostream>
using namespace std;

class Jupyter {
private:
  double xv;
  double yv;

public:
  Jupyter(double x, double y) : xv(x), yv(y) {}

  void operator+=(const Jupyter& bin {
    xv += bin.xv;
    yv += bin.yv;
  }

  void show() const {
    cout << xv << "x" << (yv >= 0 ? "+" : "") << yv << "y" << endl;
  }
};

int main() {
  double x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;

  Jupyter j1(x1, y1), j2(x2, y2);
  j1 += j2;
  j1.show();

  return 0;
}

2.某星球上有名为Nept的类,包含两个int类型的私有变量vx与vy.
若Nept对象的私有变量为2和-3,输出的形式为: 2x-3y
Nept对象自增运算符为++,功能是使两个私有变量的值分别加上3.
请补充程序完成如下功能
例 (只有第1行为输入)
(1)
2 3
5x+6y
5x+6y
(2)
-5 -6
-2x-3y
-2x-3y
请注意,main()函数或函数调用必须按如下所示编写
int main()
{
int a,b;
cin>>a>>b;
Nept v1(a,b),v2;
v2=++v1;
v1.show();
v2.show();
return 0;
}

代码如下:

#include <iostream>
using namespace std;

class Nept {
private:
  int vx;
  int vy;

public:
  Nept(int x = 0, int y = 0) : vx(x), vy(y) {}

  Nept operator++() {
    vx += 3;
    vy += 3;
    return *this;
  }

  void show() const {
    cout << vx << "x" << (vy >= 0 ? "+" : "") << vy << "y" << endl;
  }
};

int main() {
  int a, b;
  cin >> a >> b;

  Nept v1(a, b), v2;
  v2 = ++v1;

  v1.show();
  v2.show();

  return 0;
}


 

3.有一复数类名为Complex,拥有两个doulbe类型的数据成员 real imag。
请将程序补充完整(运算符重载),能够实现一个小数加复数的运算:

例1(只有第1行为输入):
1.1 2.2 3.3
(1.1,2.2)
(0,0)
(4.4,2.2)

例2(只有第1行为输入):
-2.23 34 2
(-2.23,34)
(0,0)
(-0.23,34)

请注意,main()函数或函数调用必须按如下所示编写:

int main()
{
    double a,b,d;
    cin>>a>>b>>d;
    Complex c1(a,b),c2;
    c1.display();
    c2.display();
    c2=d+c1;
    c2.display();
    return 0;
 } 

代码如下:

#include <iostream>
using namespace std;
class Complex {
private:
    double real;
    double imag;
public:
    Complex() {
        real = 0.0;
        imag = 0.0;
    }
    Complex(double r, double i) {
        real = r;
        imag = i;
    }
    Complex operator+(double num) {
        Complex result;
        result.real = real + num;
        result.imag = imag;
        return result;
    }
    Complex operator+(const Complex& c) {
        Complex result;
        result.real = real + c.real;
        result.imag = imag + c.imag;
        return result;
    }
    void display() {
        cout << "(" << real << "," << imag << ")" << endl;
    }
};

int main() {
    double a, b, d;
    cin >> a >> b >> d;
    Complex c1(a, b), c2;
    c1.display();
    c2.display();
    c2 = c1 + d;
    c2.display();
    return 0;
}

4.某星球有名为Plutu类的数据,包含两个私有变量: string name; int value;
Plutu类对象数据可以根据其name数据成员的大小进行比较
请补充程序完成如下功能:
(1) (只有第1行为输入)
wang 3 li 5
wang:3
li:5
true
(2)(只有第1行为输入) :
li 3 wnang 8
li:3
wnang:8
false
请注意,main(函数或函数调用必须按如下所示编写:
int main()
{
string s1,s2;
int k1,k2;
cin>>s1>>k2>>s2>>k2;
Plutu p1(s1,k1),p2(s2,k2);
p1.show();
p2.show();
cout<<(p1>=p2)<<endl;
return 0:
}

代码如下:

#include <iostream>
using namespace std;

class Plutu
{
private:
    string name;
    int value;

public:
    Plutu(string n, int v)
 {
        name = n;
        value = v;
    }

    string operator>=(const Plutu& other)
 {
        if(name>=other.name)
   return "true";
  else
   return "false";
    }

    void show()
 {
        cout << name << ":" << value << endl;
    }
};

5.某星球球上有名为Mers的类,包含两个doulbe类型的私有变量av与bv。
若Mers对象的私有变量为2.2和3.3,输出的形式为: 2.2a+3.3b
两个Mers对象m1与m2相减的规律为: (x1a+y1b)-(x2a+y2b)=(x1-x2)a+(y1-y2)b
m1-=m2;与m1=m1-m2;等价

请补充程序完成如下功能:
例:
(1)输入: 1.1 2.2 3.3 4.4 输出:-2.2a-2.2b
(2)输入: 1.1 2.2 3.3 -4.4 输出: -2.2a+6.6b
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
double a1,b1,a2,b2;
cin>>a1>>b1>>a2>>b2;

Mers m1(a1,b1),m2(a2,b2);
m1-=m2;
m1.show();
return 0;
}

#include <iostream>
using namespace std;

class Mers {
private:
    double av;
    double bv;
public:
    Mers(double a, double b) {
        av = a;
        bv = b;
    }
    void operator-=(const Mers& bin) {
        av -= bin.av;
        bv -= bin.bv;
    }
    void show() {
        cout << av << "a";
        if (bv >= 0) {
            cout << "+";
        }
        cout << bv << "b" << endl;
    }
};

int main() {
    double a1, b1, a2, b2;
    cin >> a1 >> b1 >> a2 >> b2;
    Mers m1(a1, b1), m2(a2, b2);
    m1 -= m2;
    m1.show();
    return 0;
}

二、复数的加、减、乘

1.从键盘上输入两个复数的实数部分与虚数部分,计算它们的并输出。
  主函数如下所示,请你创建复数类,写出运算符重载的程序。
注意:
(1) 实部与虚部均为0时,只输出实部;
(2) 实部为0,虚部不为0时,只输出虚部;
(3) 实部不为0,虚部为0时,只输出实部;
(4) 输出一个复数时,均用“( )”括起来;
例:
(1) 输入: 1.1 2 3 4 输出: (1.1+2i)+(3+4i)=(4.1+6i)
(2) 输入: 0 0 0 0输出: (0)+(0)=(0)
(3) 输入: 1 0 1 0输出: (1)+(1)=(2)
(4) 输入: 0 1 0 1输出: (1i)+(1i)=(2i)
(5) 输入: 1 -2 -1 3 输出: (1-2i)+(-1+3i)=(1i)
(6) 输入: 0 1 0 -1 输出: (1i)+(-1i)=(0)
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
    double a1,a2,b1,b2;
    cin>>a1>>a2>>b1>>b2;
    Complex A1(a1,a2),A2(b1,b2),A3;

    A3=A1+A2;
    A1.print();
    cout<<"+";
    A2.print();
    cout<<"=";
    A3.print();
    return 0;
}
代码如下:

#include <iostream>
using namespace std;

class Complex {
private:
  double real;
  double imaginary;

public:
  Complex(double r = 0, double i = 0) : real(r), imaginary(i) {}

  Complex operator+(const Complex& other) const {
    double resultReal = real + other.real;
    double resultImaginary = imaginary + other.imaginary;
    return Complex(resultReal, resultImaginary);
  }

  void print() const {
    if (real != 0 && imaginary != 0) {
      cout << "(" << real << (imaginary > 0 ? "+" : "") << imaginary << "i)";
    } else if (real != 0) {
      cout << "(" << real << ")";
    } else if (imaginary != 0) {
      cout << "(" << imaginary << "i)";
    } else {
      cout << "(0)";
    }
  }
};

int main() {
  double a1, a2, b1, b2;
  cin >> a1 >> a2 >> b1 >> b2;
  Complex A1(a1, a2), A2(b1, b2), A3;

  A3 = A1 + A2;
  A1.print();
  cout << "+";
  A2.print();
  cout << "=";
  A3.print();

  return 0;
}

2.从键盘上输入两个复数的实数部分与虚数部分,计算它们的并输出。
  主函数如下所示,请你创建复数类,写出运算符重载的程序。
注意:
(1) 实部与虚部均为0时,只输出实部;
(2) 实部为0,虚部不为0时,只输出虚部;
(3) 实部不为0,虚部为0时,只输出实部;
(4) 输出一个复数时,均用“( )”括起来;
例:
(1) 输入: 1.1 2 3 4 输出: (1.1+2i)-(3+4i)=(1.9-2i)
(2) 输入: 0 0 0 0输出: (0)-(0)=(0)
(3) 输入: 1 0 1 1输出: (1)-(1+1i)=(-1i)
(4) 输入: 2 1 0 1输出: (2+i)-(1i)=(2)
(5) 输入: 1 -2 -1 3 输出: (1-2i)-(-1+3i)=(2-5i)
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
    double a1,a2,b1,b2;
    cin>>a1>>a2>>b1>>b2;
    Complex A1(a1,a2),A2(b1,b2),A3;

    A3=A1-A2;
    A1.print();
    cout<<"-";
    A2.print();
    cout<<"=";
    A3.print();
    return 0;
}

代码如下:

#include <iostream>

using namespace std;

class Complex {

private:

  double real;

  double imaginary;

public:

  Complex(double r = 0, double i = 0) : real(r), imaginary(i) {}

  Complex operator-(const Complex& other) const {

    double resultReal = real - other.real;

    double resultImaginary = imaginary - other.imaginary;

    return Complex(resultReal, resultImaginary);

  }

  void print() const {

    if (real != 0 && imaginary != 0) {

      cout << "(" << real << (imaginary > 0 ? "+" : "") << imaginary << "i)";

    } else if (real != 0) {

      cout << "(" << real << ")";

    } else if (imaginary != 0) {

      cout << "(" << imaginary << "i)";

    } else {

      cout << "(0)";

    }

  }

};

int main() {

  double a1, a2, b1, b2;

  cin >> a1 >> a2 >> b1 >> b2;

  Complex A1(a1, a2), A2(b1, b2), A3;

  A3 = A1 - A2;

  A1.print();

  cout << "-";

  A2.print();

  cout << "=";

  A3.print();

  return 0;

}

3.从键盘上输入两个复数的实数部分与虚数部分,计算它们的并输出。
主函数如下所示,请你创建复数类,写出运算符重载的程序。
复数相乘公式: (a+bi)*(c+di)=(a*c-b*d)+(a*d+b*c)i
注意:
(1) 实部与虚部均为0时,只输出实部;
(2) 实部为0,虚部不为0时,只输出虚部
(3)实部不为0,虚部为0时,只输出实部
(4) 输出一个复数时,均用“( )”括起来
例:
(1) 输入: 1.1 2 3 4 输出: (1.1+2i)*(3+4i)=(-4.7+10.4i)
(2) 输入: 0 0 20 0输出: (0)*(20+3i)=(0)
(3) 输入: 2 0 10 -1输出: (2)*(10-1i)=(20-2i)
(4) 输入: 2 1 0 1输出: (2+1i)*(1i)=(-1+2i)
(5) 输入: 1 -2 -1 3 输出: (1-2i)*(-1+3i)=(5+5i)
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
    double a1,a2,b1,b2;
    cin>>a1>>a2>>b1>>b2;
    Complex A1(a1,a2),A2(b1,b2),A3;

    A3=A1*A2;
    A1.print();
    cout<<"*";
    A2.print();
    cout<<"=";
    A3.print();
    return 0;
}

代码如下:

#include <iostream>
using namespace std;
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}
Complex operator*(const Complex& bin) const {
    double resulbin1 = real * bin.real - imag * bin.imag;
    double resultbin2 = real * bin.imag + imag * bin.real;
    return Complex(resulbin1, resultbin2);
}
void print() const {
    if (real != 0 && imag != 0) {
    cout << "(" << real << (imag > 0 ? "+" : "") << imag << "i)";
} else if (real != 0) {
    cout << "(" << real << ")";
} else if (imag != 0) {
    cout << "(" << imag << "i)";
} else {
    cout << "(0)";
}
}
};

int main() {
double a1, a2, b1, b2;
cin >> a1 >> a2 >> b1 >> b2;
Complex A1(a1, a2), A2(b1, b2), A3;
A3 = A1 * A2;
A1.print();
cout << "*";
A2.print();
cout << "=";
A3.print();
return 0;
}

三、模板

1. 请将程序补充完整,进行减的运算,并满足下面条件
例 (只有第1行为输入) :
2 3 4.4 5.5
-1
-1.1
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
    int a,b;
    double c,d;
    cin>>a>>b>>c>>d;
    cout<<jian(a,b)<<endl;
    cout<<jian(c,d)<<endl;
    return 0;
}

代码如下:

#include<iostream>

using namespace std;

template <typename T>

T jian(T Bin1,T Bin2){

return Bin1-Bin2;

}

2.请将程序补充完整,进行加的运算,并满足下面条件。
例 (只有第1行为输入) :
2 3 4.4 5.5
5
9.9
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
int a,b;
double c,d;
cin>>a>>b>>c>>d;
cout<<add(a,b)<<endl;
cout<<add(c,d)<<endl;

return 0;
}

代码如下:

#include <iostream>

using namespace std;

template<typename T>

T mul(T bin1, T bin2)

{

    return bin1 + bin2  ;

}

int main()

{

int a,b;

double c,d;

cin>>a>>b>>c>>d;

cout<<jian(a,b)<<endl;

cout<<jian(c,d)<<endl;

return 0;

}

3.请将程序补充完整,计算三个数和
考察知识点:函数模板。
例 (只有第1行为输入):
1 2 3 1.1 2.2 3.3
sum(int):6
sum(double):6.6
请注意,main0函数或函数调用必须按如下所示编写:
int main()
{
int a,b,c;
double x,y,z;
cin>>a>>b>>c>>x>>y>>z;
cout<<"sum(int):"<<sum(a,b,c)<<endl;
cout<<"sum(double):"<<sum<double>(x,y,z)<<endl;
return 0;
}

代码如下:

#include <iostream>
using namespace std;

template<typename T>
T sum(T a, T b, T c) 
{
    return a + b + c;
}

int main()

{

int a,b,c;

double x,y,z;

cin>>a>>b>>c>>x>>y>>z;

cout<<"sum(int):"<<sum(a,b,c)<<endl;

cout<<"sum(double):"<<sum<double>(x,y,z)<<endl;

return 0;

}

4.请将程序补充完整,进行乘法的运算,并满足下面条件
例 (只有第1行为输入) :
2 3 4.4 5.5
6
24.2
请注意,main()函数或函数调用必须按如下所示编写
int main()
{
int a,b;
double c,d;
cin>>a>>b>>c>>d;
cout<<mul(a,b)<<endl;
cout<<mul(c,d)<<endl;

return 0;
}

程序如下:

#include <iostream>
using namespace std;
template<typename T>
T mul(T bin1, T bin2)
{
    return bin1 *bin2 ;
}

int main()
{
int a,b;
double c,d;
cin>>a>>b>>c>>d;
cout<<mul(a,b)<<endl;
cout<<mul(c,d)<<endl;
return 0;
}

5.请将程序补充完整,计算三个数乘积。
1 2 3 1.1 2.2 3.3 //输入
mul(int):6//输出
mul(double):7.986//输出
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
int a,b,c;
double x,y,z;
cin>>a>>b>>c>>x>>y>>z;
cout<<"mul(int):"<<mul<int>(a,b,c)<<endl;
cout<<"mul(double):"<<mul(x,y,z)<<endl;
return 0;
}

代码如下:

#include <iostream>
using namespace std;
template<typename T>
T mul(T a, T b, T c)
{
    return a * b * c;
}

int main()
{
    int a, b, c;
    double x, y, z;
    cin >> a >> b >> c >> x >> y >> z;
    cout << "mul(int):" << mul<int>(a, b, c) << endl;
    cout << "mul(double):" << mul(x, y, z) << endl;
    return 0;
}

6.请将程序补充完整,输入长方体的边长,计算体积并输出
考察知识点: 函数模板
例 (第1行为输入,后2行为输出):
1 2 3 4.4 5.5 6.6
volume(int):6
volume(float):159.72
请注意,main()函数或函数调用必须按如下所示编写!
int main()
{
    int a,b,c;
    double x,y,z;
    cin>>a>>b>>c>>x>>y>>z;
    cout<<"volume(int):"<<area(a,b,c)<<endl;
    cout<<"volume(float):"<<area<float>(x,y,z)<<endl;
    return 0;
}

代码如下:

#include<iostream>
using namespace std;
template <typename T>
T volume(T length, T width, T height) {
  return length * width * height;
}

int main() {
  int a, b, c;
  double x, y, z;
  cin >> a >> b >> c >> x >> y >> z;
  cout << "volume(int):" << volume(a, b, c) << endl;
  cout << "volume(float):" << volume<double>(x, y, z) << endl;
  return 0;
}

7.请将程序补充完整,满足下面的程序运行.
考察知识点:
函数模板
例 (只有第1行为输入):
1 2 3 1.1 2.2 3.3
max(int):3
max(double):3.3
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
int a,b,c;
double x,y,z;
cin>>a>>b>>c>>x>>y>>z;
cout<<"max(int):"<<max(a,b,c)<<endl;
cout<<"max(double):"<<max<double>(x,y,z)<<endl;
return 0;
}

代码如下:

#include <iostream>
using namespace std;

template<typename T>
T max(T a, T b, T c) {
    T maxVal = a;
    if (b > maxVal) {
        maxVal = b;
    }
    if (c > maxVal) {
        maxVal = c;
    }
    return maxVal;
}

int main()
{
int a,b,c;
double x,y,z;
cin>>a>>b>>c>>x>>y>>z;
cout<<"max(int):"<<max(a,b,c)<<endl;
cout<<"max(double):"<<max<double>(x,y,z)<<endl;
return 0;
}

四、抽象类

1.(1)某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元
(2)定义教师抽象类(Teacher) ,公有派生不同职称的教师类(ProfessorAssociateProfessor),
编写程序求若于个教师的月工资。
(3) 其中教师类包含3个保护数据成员: char name[20]; int coursetime; float salary;
它们分别是“姓名”、“每月课时数”“工资”.
例 (第1行为输入) :
Professor:Wang wei,salary:5000
Professor:Wang wei,salary:6600
AssociateProfessor:Li Ming,salary:3000
AssociateProfessor:Li Ming,salary:4440
int main()

    Teacher *pt;
    Professor wl("Wang wei",32);
    
    pt=&wl;
    pt->print();  //输出姓名与没加课时费的工资 
    pt->pay();      //计算工资 
    pt->print();  //输出姓名和工资
    
    AssociateProfessor lm("Li Ming",48);
    pt=&lm;
    pt->print();  //输出姓名与没加课时费的工资 
    pt->pay();      //计算工资 
    pt->print();  //输出姓名和工资 
    return 0;
}

代码如下:

#include <iostream>
#include <cstring>
using namespace std;
class Teacher
{
protected:
    char name[20];
    int coursetime;
    float salary;
public:
    Teacher(const char* n, int ct)
 {
        strcpy(name, n);
        coursetime = ct;
        salary = 0.0;
    }
    virtual void pay() = 0;

    virtual void print()=0;
};
class Professor : public Teacher
{
public:
    Professor(const char* n, int ct) : Teacher(n, ct)
 {
  salary = 5000;
 }
   void pay()
 {
        salary = 5000 + coursetime * 50;
    }
  
    void print()
 {
        cout << "Professor:" << name << ",salary:" << salary << endl;
    }
};
class AssociateProfessor : public Teacher
{
public:
    AssociateProfessor(const char* n, int ct) : Teacher(n, ct)
 {
  salary = 3000;
 }
    void pay()
 {
        salary = 3000 + coursetime * 30;
    }

    void print()
 {
        cout << "AssociateProfessor:" << name << ",salary:" << salary << endl;
    }
};

int main()

{

    Teacher *pt;

    Professor wl("Wang Wei",32);

    pt=&wl;

    pt->print();  //输出姓名与没加课时费的工资

    pt->pay();      //计算工资

    pt->print();  //输出姓名和工资

    AssociateProfessor lm("Li Mei",48);

    pt=&lm;

    pt->print();  //输出姓名与没加课时费的工资

    pt->pay();      //计算工资

    pt->print();  //输出姓名和工资

    return 0;

}

2.(1)某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元
(2)定义教师抽象类(Teacher) ,公有派生不同职称的教师类(ProfessorLectuer),
编写程序求若于个教师的月工资。
(3) 其中教师类包含3个保护数据成员: char name[20]; int coursetime; float salary;
它们分别是“姓名”、“每月课时数”“工资”.
例 (第1行为输入) :
Professor:Wang wei,salary:5000
Professor:Wang wei,salary:6600
AssociateProfessor:Li Ming,salary:2500
AssociateProfessor:Li Ming,salary:3700
int main()

    Teacher *pt;
    Professor wl("Wang Lei",32);
    
    pt=&wl;
    pt->print();  //输出姓名与没加课时费的工资 
    pt->pay();      //计算工资 
    pt->print();  //输出姓名和工资
    
    AssociateProfessor lm("Li Mei",48);
    pt=&lm;
    pt->print();  //输出姓名与没加课时费的工资 
    pt->pay();      //计算工资 
    pt->print();  //输出姓名和工资 
    return 0;
}

代码如下:

#include<iostream>
#include<cstring>
using namespace std;
class Teacher{
    protected:
    char name[20];
    int coursetime;
    float salary;
    public:
    Teacher(char*n,int c)
    {
        strcpy(name,n);
        coursetime = c;
        salary = 0.0;
    }
    virtual void print()=0;
    virtual void pay()=0;
};
class Professor:public Teacher{
    public:
    Professor(char* n,int c):Teacher(n,c){
        salary=5000;
    }
    void pay(){
        salary = 5000+coursetime*50;
    }
    void print(){
        cout<<"Professor:Wang Lei,salary:"<<salary<<endl;
    }
};
class Lectuer:public Teacher{
    public:
    Lectuer(char* n,int c):Teacher(n,c){
        salary=2500;
    }
    void pay(){
        salary = 2500+coursetime*25;
    }
    void print(){
        cout<<"Lectuer:Li Mei,salary:"<<salary<<endl;
    }
};

int main(){

    Teacher*pt;

    Professor p1("Wang Lei",32);

    pt = &p1;

    pt->print();

    pt->pay();

    pt->print();

    Lectuer p2("Li Mei",48);

    pt = &p2;

    pt->print();

    pt->pay();

    pt->print();

    return 0;

}

3.(1)某学校对教师每月工资的计算规定如下:固定工资+课时补贴。教授的固定工资为5000元,每个课时补贴50元。
副教授的固定工资为3000元,每个课时补贴30元
(2)定义教师抽象类(Teacher) ,公有派生不同职称的教师类(Professor、AssociateProfessor),
编写程序求若于个教师的月工资。
(3) 其中教师类包含3个保护数据成员:
stirng name; int hours; float salary;
它们分别是“姓名”、“每月课时数”“工资”.
例 (第1行为输入) :

WangWei 20 LiLei 20
Professor:WangWei,20,6000
AssociateProfessor:LiLei,20,3600

请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
Teacher *pt;
string str1,str2;
int n1,n2;
cin>>str1>>n1>>str2>>n2;
Professor ww(str1,n1,5000);

pt=&ww;
pt->pay();   //计算工资,输出姓名、课时、工资

AssociateProfessor lm(str2,n2,3000);
pt=&lm;
pt->pay();   //计算工资,输出姓名、课时、工资
return 0;
}

代码如下:

#include <iostream>
#include <string>
using namespace std;
class Teacher {
protected:
    string name;
    int hours;
    float salary;
public:
Teacher(string n, int h) : name(n), hours(h) {}
virtual void pay() = 0;
};
class Professor : public Teacher {
public:
Professor(string n, int h, float s) : Teacher(n, h) {
    salary = s + h * 50;
}
void pay() {
    cout << "Professor:" << name << "," << hours << "," << salary << endl;
}
};
class AssociateProfessor : public Teacher {
    public:
    AssociateProfessor(string n, int h, float s) : Teacher(n, h) {
    salary = s + h * 30;
}
void pay() {
    cout << "AssociateProfessor:" << name << "," << hours << "," << salary << endl;
}
};

int main() {

    Teacher *pt;

    string str1, str2;

    int n1, n2;

    cin >> str1 >> n1 >> str2 >> n2;

    Professor ww(str1, n1, 5000);

    pt = &ww;

    pt->pay();

    AssociateProfessor lm(str2, n2, 3000);

    pt = &lm;

    pt->pay();

    return 0;

}

4.(1)已知Shape类为抽象类,包含一个protected的double类型变量 x;
(2)
Circle类与lnSquare类均从Shape类公有派生;
(3)若圆的半径为R,其内接正方形的信息如下图所示
请根据运行结果与main函数将程序补充完整
计算时元取3.14,2的平方根取1.414.
例1 (只有第1行为输入) :
1.2
Circle area=4.5216
Circle perimeter=7.536
InSquare area=2.88
InSquare perimeter=6.7872
请注意,main0函数或函数调用必须按如下所示编写:
int main()
{
double r;
cin>>r;
Shape*p;
Circle c(r);
InSquare is(r);

p=&c;
p->area(); //计算圆的面积并输出
P->Perimeter(); //计算圆的周长并输出
p=&is;
p->area(); //计算圆内接正方形的面积并输出
p->perimeter(); //计算圆内接正方形的周长并输出
return 0;
}

代码如下:

#include <iostream>
#include <cmath>
using namespace std;
class Shape {
protected:
double R;
public:
Shape(double R1):R(R1){}
virtual void area()=0;
virtual void perimeter()=0;
};
class Circle: public Shape {
    public:
    Circle(double R1):Shape(R1){}
    void area(){
    double A = 3.14 * R * R;
    cout << "Circle area=" << A << endl;
}
void perimeter(){
    double P = 2 * 3.14 * R;
    cout << "Circle perimeter=" << P << endl;
}
};
class InSquare: public Shape {
    public:
    InSquare(double R1):Shape(R1){}
    void area(){
    double A = R * R * 2;
    cout << "InSquare area=" << A << endl;
}
void perimeter(){
    double er=1.414;
    double P = 4* R * er;
    cout << "InSquare perimeter=" << P << endl;
}
};

int main() {

double r;

cin >> r;

Shape* p;

Circle bin1(r);

InSquare bin2(r);

p = &bin1;

p->area();

p->perimeter();

p = &bin2;

p->area();

p->perimeter();

return 0;

}

5.(1) 已知Figure类为抽象类,包含两个protected的double类型变量 x与y;
(2) Square类与Circle类均从Figure类公有派生;
(3) 计算中要用到π,取值3.14.
请根据运行结果与main函数将程序补充完整。
例1 (只有第1行为输入) :
2.2 3.3
weight:2.2,height:3.3
area=7.26
radius:2.2
area=15.1976
请注意,main0函数或函数调用必须按如下所示编写:
int main()
{
Figure *p;
double a,b;
Square s(a,b);
Circle c(a);
p =&s;
p->area(); ///输出矩形的长、宽,计算面积并输出
p=&c;
p->area(); //输出圆的半径,计算面积并输出

return 0;
}

代码如下:

#include <iostream>
#include <cmath>
using namespace std;
const double pi= 3.14;
class Figure {
protected:
    double x, y;
public:
    virtual void area() = 0;
};
class Square : public Figure {
public:
    Square(double a, double b) {
        x = a;
        y = b;
        cout << "weight:" << x << ",height:" << y << endl;
    }
    void area() override {
        double area = x * y;
        cout << "area=" << area << endl;
    }
};
class Circle : public Figure {
public:
    Circle(double r) {
        x = r;
        cout << "radius=" << x << endl;
    }
    void area() override {
        double area = pi * pow(x, 2);
        cout << "area=" << area << endl;
    }
};

int main() {

    Figure *p;

    double a, b;

    cin >> a >> b;

    Square s(a, b);

    p = &s;

    p->area();

    Circle c(a);

    p = &c;

    p->area();

    return 0;

}

五、其他类

1.请创建一个长方形类,输入长与宽,能够计算面积并输出。
例 (只有第1行为输入) :
3.3 2.0
0,0
3.3,2
0
6.6
请注意,main()函数或函数调用必须按如下所示编写:
int main()
{
double x,y;
cin>>x>>y;
Rectangle r1,r2(x,y);
r1.show();
r2.show();
cout<<r1.aera()<<endl;
cout<<r2.aera()<<endl;
return 0;
}

代码如下:

#include <iostream>
using namespace std;
class Rectangle {
private:
double length, width;
public:
Rectangle() {
length = 0.0;
width = 0.0;
}
Rectangle(double l, double w) {
    length = l;
    width = w;
}
void show() {
    cout << length << "," << width << endl;
}
double area() {
    return length * width;
}
};

int main() {

double x, y;

cin >> x >> y;

Rectangle r1, r2(x, y);

r1.show();

r2.show();

cout << r1.area() << endl;

cout << r2.area() << endl;

return 0;

}

2.定义一个三角形类,输入三条边,能够计算其面积并输出
例 (只有第1行为输入) :
3.3 4.4 5.5
0,0,0
3.3,4.4,5.5
0
7.26
请注意,main()函数或函数调用必须按如下所示编写
int main()
{
double a,b;
cin>>a>>b>>c;
Triangle t1,t2(a,b,c);
t1.show();
t2.show();
cout<<t1.area()<<endl;
cout<<t2.area()<<endl;

return 0;
}

代码如下:

#include <iostream>
#include <cmath>
using namespace std;
class Bin{
    private:
    double a, b, c;
    public:
    Bin(){
    a = 0;
    b = 0;
    c = 0;
}
Bin(double x, double y, double z){
    a = x;
    b = y;
    c = z;
}
void show(){
cout << a << "," << b << "," << c << endl;
}
double area(){
double p = (a+b+c) / 2;
double s = sqrt(p * (p-a) * (p-b) * (p-c));
return s;
}
};

int main()

{

    double a,b,c;

    cin>>a>>b>>c;

    Bin t1,t2(a,b,c);

    t1.show();

    t2.show();

    cout<<t1.area()<<endl;

    cout<<t2.area()<<endl;

    return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值