面向对象程序设计-第一章:面向对象程序设计基础

简介: 面向对象程序设计-第一章:面向对象程序设计基础

1. 分数类


image.png

Fract.cpp

#include"Fract.h"
#include<iostream>
using namespace std;
int Fract::ged(int m, int n)
{
  int v,a=m,b=n;
  v = a % b;
  while (v != 0)
  {
    a = b;
    b = v;
    v = a % b;
  }
  return b;
}
Fract Fract::add(Fract f2)
{
  int v;
  v = ged(den, f2.den);
  int com;
  com = den * f2.den / v;
  Fract f3(0,1);
  num = num * (com / den);
  f2.num = f2.num * (com / f2.den);
  f3.num = num + f2.num;
  f3.den = com;
  v = ged(f3.num, f3.den);
  f3.num = f3.num / v;
  f3.den = f3.den / v;
  return f3;
}

main.cpp

#include"Fract.h"
#include<iostream>
using namespace std;
int main()
{
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  Fract f1(a, b);
  Fract f2(c, d);
  Fract f3(0,1);
  f3 = f1.add(f2);
  f3.show();
  return 0;
}

2.圆类

【问题描述】编写圆的类

【样例输入】【样例输出】

请输入圆的半径:23

圆的面积为:1661.06

圆的周长为:144.44

const float PI = 3.14; 
#pragma once
class Circle
{
public:
  Circle(double _r)
  {
    r = _r;
  }
  double getArea()
  {
    return 3.14 * r * r;
  }
  double getGirth()
  {
    return 2 * 3.14 * r;
  }
private:
  double r;
};
#include<iostream>
using namespace std;
int main()
{
  float r;
  cout << "请输入圆的半径:";
  cin >> r;                                     //从键盘接受半径的值
    Circle c1(r);                            //将值赋值给半径并调用定义好的函数
    cout<<"圆的面积为:"<<c1.getArea()<<endl;
    Circle c2(r);
    cout<<"圆的周长为:"<<c2.getGirth()<<endl;
}

3.编写矩形类

【问题描述】编写程序求长方形的周长和面积

【输入形式】

【输出形式】

【样例输入】【样例输出】

Input the Length and Width: 12 23

The Area is:276

The Premeter:70

#include <iostream>
class Rectangle                           
{
public:
  Rectangle(void);
   Rectangle(float length,float width);  
  ~Rectangle(void);         
      float getArea();                  
      float getGirth();                  
private:
      float mLength;                    
    float mWidth;                      
};
Rectangle::Rectangle()
{
  mLength = 0; mWidth = 0;
}
Rectangle::Rectangle(float  length, float  width)
{
  mLength = length;
  mWidth = width;
}
float Rectangle::getArea()
{
  return mLength * mWidth;
}
float  Rectangle::getGirth()
{
  return 2 * (mLength + mWidth);
}
Rectangle::~Rectangle(void)
{
}
#include<iostream>
using namespace std;
int main()
{
  float m, n;                                     
  cout << "Input the Length and Width: ";
  cin >> m >> n;                                 
  Rectangle r1(m,n);     
    cout<<"The Area is: "<<r1.getArea()<<endl;
    Rectangle r2(m,n);
    cout<<"The Perimeter: "<<r2.getGirth()<<endl;
}


目录
相关文章
|
1月前
|
Java 物联网 测试技术
Java面向对象程序设计3面向对象基础
Java面向对象程序设计3面向对象基础
169 0
|
1月前
|
算法 开发者
程序设计 (4)
程序设计 (4)
13 0
|
5月前
|
C++
20 C++ - 面向对象程序设计案例
20 C++ - 面向对象程序设计案例
53 0
|
1月前
|
C++
25面向对象的程序设计
25面向对象的程序设计
13 1
|
11月前
|
存储 Java
面向对象程序设计概述
面向对象程序设计概述
162 0
|
11月前
|
程序员 测试技术 C语言
c++面向对象程序设计入门
c++面向对象程序设计入门
139 0
|
存储 机器学习/深度学习 编译器
面向对象程序设计 C++总结笔记(1)
理解面向对象程序设计的基本原理,掌握面向对象技术的基本概念和封装性、继承性和多态性,能够具有面向对象程序设计思想。掌握C++语言面向对象的基本特性和C++语言基础知识,能够使用C++语言进行计算机工程领域复杂工程问题的表述,能够进行C++程序阅读和分析。
130 0
|
算法 JavaScript Java
面向对象程序设计
面向对象程序设计是一种编程范式或编程风格。 面向对象的程序是由类和对象组成的(以类和对象作为组织代码的基本单元),并将封装、抽象、继承、多态这四个特性,作为程序设计和实现的基础。
面向对象程序设计第三章
面向对象程序设计第三章
52 0
面向对象程序设计-第二章
面向对象程序设计-第二章
37 0
面向对象程序设计-第二章