【编码狂想】解谜OOP:通过实战揭秘面向对象编程的奥秘

简介: 【编码狂想】解谜OOP:通过实战揭秘面向对象编程的奥秘



🌐第一部分 引用篇

😎1.1 编写函数实现两数交换(引用方式)

描述

编写一个函数,实现两个整数的交换,要求采用引用的方式实现。

输入描述:

键盘输入 2 个整数 m 和 n

输出描述:

输出交换后 m 和 n 的值,中间使用空格隔开

示例1

输入:
10
20
输出:
20 10

💡解决如下:

#include <iostream>
#include <utility>
using namespace std;
//引用交换
void swap(int &a,int &b){
    int t=a;
    a=b;
    b=t;
}
int main(){
    int a,b;
    cin>>a;
    cin>>b;
    int &aa=a;
    int &bb=b;
    swap(aa,bb);
    cout<<a<<" "<<b<<endl;
    return 0;
}

😎1.2 编写函数实现字符串翻转(引用方式)

描述

编写一个函数,实现字符串的翻转,要求采用引用的方式实现。

输入描述:

输入一个字符串

输出描述:

输出翻转后的字符串

示例1

输入:
abc
输出:
cba

💡解决如下:

#include <iostream>
#include <cstring>
using namespace std;
//翻转字符串
void func(string &s){
    int slen=s.length();
    char c;
    for(int i=0;i<slen/2;i++){
        c=s[i];
        s[i]=s[slen-i-1];
        s[slen-i-1]=c;
    }
}
int main(){
    string s;
    getline(cin,s);
    string &ss=s;
    func(ss);
    cout<<s<<endl;
    return 0;
}

🌐第二部分 封装篇

😎2.1 设计立方体类

描述

采用封装的思想设计一个立方体类(Cube),成员变量有:长(length)、宽(width)、高(height),都为 int 类型;

成员方法有:获取表面积的方法(getArea)、获取体积的方法(getVolume)。

输入描述:

键盘输入立方体的长(length)、宽(width)、高(height),都为 int 类型,范围[1, 100]

输出描述:

输出立方体的长(length)、宽(width)、高(height)、面积、体积,中间使用空格隔开。

例如:3 4 5 60

示例1

输入:
3
4
5
输出:
3 4 5 94 60

💡解决如下:

#include <iostream>
using namespace std;
//Cube类
class Cube{
private:
    int length;
    int width;
    int height;
public:
    Cube(int l,int w,int h){//构造函数1
        length=l;
        width=w;
        height=h;
    }
    Cube(){}//构造函数2
    int getArea(){
        return (length*width+length*height+width*height)*2;
    }
    int getVolume(){
        return length*width*height;
    }
    void Disp(){
        cout<<length<<" "<<width<<" "<<height<<" "<<getArea()<<" "<<getVolume()<<endl;
    }
};
int main(){
    int l,w,h;
    cin>>l;
    cin>>w;
    cin>>h;
    Cube cube(l,w,h);
    cube.Disp();
    return 0;
}

😎2.2 点和圆的关系

描述

有圆类(Circle)和点类(Pointer),请在圆类中实现一个 isPointerInCircle方法,该方法传入一个点类对象,判断点和圆的关系,并在该方法中输出。

点类(Pointer):

       成员变量:x轴坐标(int x)  y轴坐标(int y)

       成员方法:成员变量的公共访问方法

圆类(Circle):

       成员变量:圆心(Point center)  半径(int radius)

       成员方法:成员变量的公共访问方法    判断点和圆关系的方法(isPointerInCircle)

点和圆的关系:

       1.点在圆外

       2.点在圆上

       3.点在圆内

 

输入描述:

键盘输入两个整数,分别是点的 x 坐标和 y 坐标(圆的参数已经给定)

输出描述:

圆类中实现 isPointInCircle 方法,在该方法中输出点和圆的关系,

如果点在圆外,则输出“out”;

如果点在圆上,则输出“on”;

如果点在圆内,则输出“in”。

示例1

输入:
5
5
输出:
on

💡解决如下:

#include <iostream>
#include <cmath>
using namespace  std;
//点类
class Point{
private:
    int x,y;
public:
    Point(int a,int b){//构造函数1
        x=a;y=b;
    }
    Point(){}//构造函数2
    int getX(){
        return x;
    }
    int getY(){
        return y;
    }
};
//圆类
class Circle{
private:
    Point center;
    int r;
public:
    Circle(Point point,int a){//构造函数1
        center=point;//拷贝
        r=a;
    }
    Circle(){}//构造函数2
    void isPointerInCircle(Point point){
        //计算点与圆心的距离
        double d=sqrt(pow((center.getX()-point.getX()),2) + pow((center.getY()-point.getY()),2));
        if(d==r) cout<<"on"<<endl;
        else if (d>=r) cout<<"out"<<endl;
        else cout<<"in"<<endl;
    }
};
int main(){
    int x,y;
    cin>>x;
    cin>>y;
    Point point(x,y);
    Point point2(5,0);
    int r=5;//固定圆心(5,0),半径r=5
    Circle circle(point,r);
    circle.isPointerInCircle(point2);
    return 0;
}

😎2.3 比较长方形的面积大小

描述

给出两个长方形的长和宽,实现长方形类的一个比较面积大小的方法,判定哪个面积大。

输入描述:

输入4个整数,前两个表示第一个长方形的长和宽,后两个表示第二个长方形的长和宽。

输出描述:

如果前者面积大,输出1,否则输出0。

示例1

输入:
4 3 2 1
输出:
1

💡解决如下:

#include <iostream>
using namespace std;
//长方形类
class Rentangle{
private:
  int lenth;
  int width;
public:
  Rentangle(int l,int w){
    lenth=l;width=w;
  }
  Rentangle(){}
  int getArea(){
    return lenth*width;
  }
};
int main(){
  int l1,l2,l3,l4;
  cin>>l1>>l2>>l3>>l4;
  Rentangle rentangle1(l1,l2),rentangle2(l3,l4);
  if(rentangle1.getArea()>rentangle2.getArea()) cout<<1<<endl;
  else cout<<0<<endl;
}

😎2.4 长方形的关系

描述

给出两个长方形的长和宽(长不一定大于宽),实现长方形类的一个方法,判定前者是否能完全覆盖后者。

输入描述:

输入4个整数,前两个表示第一个长方形的长和宽,后两个表示第二个长方形的长和宽。

输出描述:

如果前者能完全覆盖后者输出"yes"否则输出"no"

示例1

输入:
5 4 2 3
输出:
yes

💡解决如下:

#include <iostream>
using namespace std;
//长方形类
class Rentangle{
private:
  int lenth;
  int width;
public:
  Rentangle(int l,int w){
    lenth=l;width=w;
  }
  Rentangle(){}
  int getL(){
    return lenth;
  }
  int getW(){
    return width;
  }
};
//判断
void Disp(Rentangle r1,Rentangle r2){
  if((r1.getL()>=r2.getL() && r1.getW()>=r2.getW()) || (r1.getW()>=r2.getL() && r1.getL()>=r2.getW())){
    cout<<"yes"<<endl;
  }
  else cout<<"no"<<endl;
}
int main(){
  int l1,l2,l3,l4;
  cin>>l1>>l2>>l3>>l4;
  Rentangle rentangle1(l1,l2),rentangle2(l3,l4);
  Disp(rentangle1,rentangle2);
  return 0;
}

🌐第三部分 构造函数篇

😎3.1 构造函数

描述

现有一个人类(Person),成员变量:姓名(string name)和年龄(int age),请给 Person 添加一个支持两个参数的构造函数,并对姓名和年龄两个成员进行初始化。

输入描述:

键盘输入用户名和年龄

输出描述:

通过 Person 类的showPerson()成员方法输出 Person 对象的姓名和年龄,中间使用空格隔开

示例1

输入:
zhangsan
20
输出:
zhangsan 20

💡解决如下:

#include <iostream>
using namespace std;
//Person类
class Person{
private:
    string name;
    int age;
public:
    Person(){}//构造函数1
    Person(string s,int n){
        name=s;age=n;
    }
    void showPerson(){
        cout<<name<<" "<<age<<endl;
    }
};
int main(){
    string s;
    int n;
    getline(cin,s);
    cin>>n;
    Person person(s,n);
    person.showPerson();
    return 0;
}

😎3.2 数组类的构造函数

描述

现在有一个数组类,请实现它的构造函数。

输入描述:

第一行一个整数n,表示数组的大小。

第二行n个整数,表示数组。

输出描述:

输出这个数组。

示例1

输入:
3
1 2 3
输出:
1 2 3

💡解决如下:

#include <iostream>
using namespace std;
//Array类
class Array{
  private:
  int n;
  int *array;
  public:
  Array(int *a,int maxSize){
    n=maxSize;
    array=new int[n];
    for(int i=0;i<n;i++){
      array[i]=a[i];
    }
  }
  Array(){}
  ~Array(){
    delete [] array;
  }
  void Disp(){
    for(int i=0;i<n;i++){
      cout<<array[i]<<" ";
    }
    cout<<endl;
  }
};
int main(){
  int maxSize;
  cin>>maxSize;
  int *a=new int[maxSize];
  for(int i=0;i<maxSize;i++){
    cin>>a[i];
  }
  Array array(a,maxSize);
  array.Disp();
  return 0;
}

🌐第四部分 拷贝构造函数篇

😎4.1 浅拷贝和深拷贝

描述

现有一个 Person 类,成员变量:姓名(string name)和年龄(int age),请给 Person 添加一个拷贝构造函数,让程序能够正确运行。

输入描述:

键盘输入用户名和年龄

输出描述:

通过 Person 类的showPerson()成员方法输出 Person 对象的姓名和年龄,中间使用空格隔开

示例1

输入:
zhangsan
20
输出:
zhangsan 20

💡解决如下:

#include <iostream>
using namespace std;
//Person类
class Person{
    private:
    string name;
    int age;
    public:
    //构造函数
    Person(){}
    Person(string s,int n){
        name=s;age=n;
    }
    //拷贝构造函数
    Person(Person &other){
        name=other.name;
        age=other.age;
    }
    //析构函数
    ~Person(){}
    void showPerson(){
        cout<<name<<" "<<age<<endl;
    }
};
void showPerson(Person p){//person赋值给p靠的就是拷贝构造函数
    p.showPerson();
}
int main(){
    string s;
    int n;
    getline(cin,s);
    cin>>n;
    Person person(s,n);
    //person.showPerson();
    showPerson(person);
    return 0;
}

😎4.2 数组类的拷贝构造函数

描述

现有一个数组类Array,请你设计一个正确的拷贝构造函数。

输入描述:

第一行一个整数n,表示数组的大小。

第二行n个整数,表示数组。

输出描述:

输出这个数组。

示例1

输入:
3
1 2 3
输出:
1 2 3

💡解决如下:

#include <iostream>
using namespace std;
//数组类
class Array{
  private:
  int *array;
  int n;
  public:
  //构造函数
  Array(){}
  Array(int *a,int maxSize){
    n=maxSize;
    array=new int[n];
    for(int i=0;i<n;i++){
      array[i]=a[i];
    }
  }
  //拷贝构造函数
  Array(const Array &other){
    n=other.n;//不要忘了
    array=new int[n];
    for(int i=0;i<n;i++){
      array[i]=other.array[i];
    }
  }
  //析构函数
  ~Array(){
    delete [] array;
  }
  void Disp(){
    for(int i=0;i<n;i++){
      cout<<array[i]<<" ";
    }
    cout<<endl;
  }
};
void func(Array a){
  a.Disp();
}
int main(){
  int maxSize;
  cin>>maxSize;
  int *a=new int[maxSize];
  for(int i=0;i<maxSize;i++){
    cin>>a[i];
  }
  Array array(a,maxSize);
  func(array);
  delete [] a;
  return 0;
}

🌐第五部分 友元篇

😎5.1 友元全局函数

描述

在现有代码的基础上,使用友元全局函数,让程序能够正常运行。

输入描述:

输出描述:

输出年龄

💡解决如下:

#include <iostream>
using namespace std;
class Person {
    // write your code here......
    friend void showAge(Person& p);//就加这一句哈哈
    public:
        Person(int age) {
            this->age = age;
        }
    private:
        int age;
};
void showAge(Person& p) {
    cout << p.age << endl;
}
int main() {
    Person p(10);
    showAge(p);
    return 0;
}

😎5.2 友元类

描述

现在有一个手机类phone与我的手机类myphone。

在现有代码的基础上,使用友元类,让程序能够正常运行。

输入描述:

输入一个整数,表示价格。

输出描述:

输出价格。

示例1

输入:
1000
输出:
1000

💡解决如下:

#include<bits/stdc++.h>
using namespace std;
class phone{
  // write your code here......
  friend class myphone;//就加这一句哈哈,这里不是friend int getprice();
  private:
    int price;
  public:
    phone(int x){
      price=x;
    }
    phone(){}
}; 
class myphone{
  private:
    phone a;
  public:
    myphone(int x):a(x){
    }
    int getprice(){
      return a.price;
    }
};
int main(){
  int p;
  cin>>p;
  myphone a(p);
  cout<<a.getprice();
  return 0;
}

🌐第六部分 继承篇

😎6.1 子类中调用父类构造

描述

有父类 Base,内部定义了 x、y 属性。有子类 Sub,继承自父类 Base。子类新增了一个 z 属性,并且定义了 calculate 方法,在此方法内计算了父类和子类中 x、y、z 属性三者的乘积。请补全子类构造方法的初始化逻辑,使得该计算逻辑能够正确执行。

输入描述:

三个整数:x, y, z

输出描述:

三个整数的乘积:x*y*z

示例1

输入:
1
2
3
输出:
6

💡解决如下:

#include <iostream>
using namespace std;
class Base {
    private:
        int x;
        int y;
    public:
        //构造函数
        Base(int x, int y) {
            this->x = x;
            this->y = y;
        }
        Base(){}
        int getX() {
            return x;
        }
        int getY() {
            return y;
        }
};
class Sub : public Base {
    private:
        int z;
    public:
        //构造函数
        Sub(int x, int y, int z) :Base(x, y) {
            // write your code here
            this->z=z;
        }
        Sub():Base(){}
        int getZ() {
            return z;
        }
        int calculate() {
            return Base::getX()*Base::getY()*z;
        }
};
int main() {
    int x, y, z;
    cin >> x;
    cin >> y;
    cin >> z;
    Sub sub(x, y, z);
    cout << sub.calculate() << endl;
    return 0;
}

😎6.2 重写子类计算逻辑

描述

在父类 Base 中定义了计算方法 calculate(),该方法用于计算两个数的乘积(X*Y)。请在子类 Sub 中重写该方法,将计算逻辑由乘法改为除法(X/Y)。注意,当分母为0时输出“Error”。

输入描述:

两个整数

输出描述:

两个整数的商(int类型,不考虑小数部分)

示例1

输入:
6
2
输出:
3

💡解决如下:

#include <iostream>
using namespace std;
class Base {
private:
    int x;
    int y;
public:
    Base(int x, int y) {
        this->x = x;
        this->y = y;
    }
    int getX() {
        return x;
    }
    int getY() {
        return y;
    }
    virtual void calculate() {
        cout << x*y << endl;
    }
};
class Sub : public Base {
public:
    Sub(int a,int b):Base(a,b){}
    void calculate() {
        if(Base::getY()==0) cout<<"Error"<<endl;//考虑分母为0的情况
        else cout << double(Base::getX()/Base::getY()) << endl;
    }
};
int main() {
    int x, y;
    cin >> x;
    cin >> y;
    Sub sub(x, y);
    sub.calculate();
    return 0;
}

😎6.3 构建长方体类

描述

现在有长方形类(rectangle),请以此为基类构建长方体类(cuboid)并实现求体积的getvolume方法。

输入描述:

输入三个整数x,y,z分别表示长宽高。

输出描述:

输出长方体的体积。

示例1

输入:
3 2 1
输出:
6

💡解决如下:

#include <iostream>
using namespace std;
class rectangle{
  private:
    int length;
    int width;
  public:
    //构造函数
    rectangle(int x,int y){
      length=x;
      width=y;
    }
    rectangle(){}
    int area(){
      return length*width;
    }
};
class cuboid:public rectangle{
  private:
    int height;
  public:
    //构造函数
    cuboid(int x,int y,int z):rectangle(x,y){
      this->height=z;
    }
    int getvolume(){
      return rectangle::area()*this->height;
    }
};
int main(){
  int x,y,z;
  cin>>x>>y>>z;
  cuboid a(x,y,z);
  cout<<a.getvolume();
  return 0;
}

😎6.4 求长方体表面积

描述

现在有长方形类(rectangle),请以此为基类构建长方体类(cuboid)并实现求表面积的area方法。

输入描述:

输入三个整数x,y,z分别表示长宽高。

输出描述:

第一行输出一个整数表示长方体的底面积。

第二行输出一个整数表示长方体的表面积。

示例1

输入:
3 2 1
输出:
6
22

💡解决如下:

#include <iostream>
using namespace std;
class rectangle{
  private:
    int length,width;
  public:
    //构造函数
    rectangle(int x,int y){
      length=x;
      width=y;
    }
    rectangle(){}
    int getlength(){
      return length;
    }
    int getwidth(){
      return width;
    }
    virtual int area(){
      return length*width;
    }
};
class cuboid:public rectangle{
  private:
    int height;
  public:
    //构造函数
    cuboid(int x,int y,int z):rectangle(x,y){
      this->height=z;
    }
    int area(){
      return 2*(rectangle::area()+rectangle::getlength()*this->height+rectangle::getwidth()*this->height);
    }
};
int main(){
  int x,y,z;
  cin>>x>>y>>z;
  cuboid a(x,y,z);
  cout<<a.rectangle::area()<<'\n'<<a.area();
  return 0;
}

🌐第七部分 多态篇

😎7.1 多态实现计算器功能

描述

完善下面的代码,使程序能够正常运行。要求 BaseCalculator 类中提供 getResult() 函数(无需实现),在 AddCalculator 类中实现两个成员相加(m_A + m_B),在 SubCalculator 类中实现两个成员相减(m_A - m_B)

输入描述:

输出描述:

程序已经给定输出

#include <iostream>
using namespace std;
class BaseCalculator {
    public:
        int m_A;
        int m_B;
        // write your code here......
};
// 加法计算器类
class AddCalculator : public BaseCalculator {
    // write your code here......
};
// 减法计算器类
class SubCalculator : public BaseCalculator {
    // write your code here......
};
int main() {
    BaseCalculator* cal = new AddCalculator;
    cal->m_A = 10;
    cal->m_B = 20;
    cout << cal->getResult() << endl;
    delete cal;
    cal = new SubCalculator;
    cal->m_A = 20;
    cal->m_B = 10;
    cout << cal->getResult() << endl;
    delete cal;
    return 0;
}

💡解决如下:

#include <iostream>
using namespace std;
class BaseCalculator {
    public:
        int m_A;
        int m_B;
        // write your code here......
        BaseCalculator() : m_A(0), m_B(0) {}
        virtual int getResult() const=0;
};
// 加法计算器类
class AddCalculator : public BaseCalculator {
    // write your code here......
    int getResult() const{
        return m_A+m_B;
    }
};
// 减法计算器类
class SubCalculator : public BaseCalculator {
    // write your code here......
    int getResult() const{
        return m_A-m_B;
    }
};
int main() {
    BaseCalculator* cal = new AddCalculator;
    cal->m_A = 10;
    cal->m_B = 20;
    cout << cal->getResult() << endl;
    delete cal;
    cal = new SubCalculator;
    cal->m_A = 20;
    cal->m_B = 10;
    cout << cal->getResult() << endl;
    delete cal;
    return 0;
}

😎7.2 多态实现求面积体积

描述

现在有长方形类(rectangle),和以此为基类构建的长方体类(cuboid),运用多态在两个类中实现getval方法,在长方形类中是求面积功能,在长方体类中是求体积功能。

输入描述:

输入三个整数�,�,�x,y,z分别表示长宽高。

输出描述:

第一行输出一个整数表示长方体的面积。

第二行输出一个整数表示长方体的体积。

示例1

输入:
3 2 1
输出:
6
6

💡解决如下:

#include <iostream>
using namespace std;
class rectangle{
  private:
    int length;
    int width;
  public:
    //构造函数
    rectangle(int x,int y){
      length=x;
      width=y;
    }
    rectangle(){}
    int getlength(){
      return length;
    }
    int getwidth(){
      return width;
    }
    // write your code here...
    virtual int getval(){
      return length*width;
    }
};
class cuboid:public rectangle{
  private:
    int height;
  public:
    //构造函数
    cuboid(int x,int y,int z):rectangle(x,y){
      this->height=z;
    }
    // write your code here...
    int getval(){
      return rectangle::getval()*this->height;
    }
};
int main(){
  int x,y,z;
  cin>>x>>y>>z;
  cuboid a(x,y,z);
  rectangle b(x,y);
  rectangle *p=&b;
  cout<<p->getval()<<'\n';
  p=&a;
  cout<<p->getval();
  return 0;
}

目录
相关文章
|
7月前
|
Java
Java面向对象编程(9)
Java面向对象编程(9)
20 0
|
7月前
|
网络协议 Java
Java面向对象编程(28)
Java面向对象编程(28)
20 0
|
7月前
|
网络协议 安全 Java
Java面向对象编程(39)
Java面向对象编程(39)
18 0
|
7月前
|
算法 安全 Java
Java面向对象编程(33)
Java面向对象编程(33)
22 0
|
7月前
|
存储 缓存 Java
Java面向对象编程(31)
Java面向对象编程(31)
20 0
|
4月前
|
并行计算 数据处理 开发者
编程范式的抉择:面向对象编程与函数式编程的对决
在当今的软件开发领域,面向对象编程(Object-Oriented Programming,OOP)和函数式编程(Functional Programming,FP)是两种重要的编程范式。本文将比较并探讨这两种编程范式的特点、优势和适用场景,以帮助开发者在编程选择上做出明智的决策。
|
5月前
|
Java 开发者
Java面向对象编程的4个主要概念
Java面向对象编程的4个主要概念
44 3
|
7月前
|
算法 Java 测试技术
Java面向对象编程(49)
Java面向对象编程(49)
25 0
|
7月前
|
人工智能 网络协议 安全
Java面向对象编程(47)
Java面向对象编程(47)
24 0
|
7月前
|
存储 安全 Java
Java面向对象编程(26)
Java面向对象编程(26)
22 1