zoukankan      html  css  js  c++  java
  • 面向对象实验一(类与对象)

    一、实验目的
    1、掌握类和对象的定义和使用。
    2、掌握友元函数的定义和使用。
    3、理解并掌握常量的定义和使用。
    4、掌握静态的定义和使用。

    二、实验内容
    1、给定三个数21,15,22,设计内联函数int max(int a, int b)求两个数的最大数,输出结果“在21 15 22之中最大的是:22”。

     1 #include<iostream>
     2 using namespace std;
     3 
     4 inline int max(int a,int b);
     5 
     6 int main()
     7 {
     8     int a=21,b=15,c=22,d,e;
     9     d=max(a,b);
    10     e=max(d,c);
    11     cout<<"在21,15,22之中最大的是:"<<e<<endl;
    12     return 0;
    13 }
    14 
    15 int max(int a,int b)
    16 {
    17    return (a>b)?a:b;
    18 }

     

    2、设计重载函数overload,如果输入整数5,则输出5,如果输入字符'a',则输出字符'b'。

     1 #include<iostream>
     2 using namespace std;
     3 
     4 void overload (int a);
     5 void overload (char b);
     6 
     7 int main()
     8 {
     9     int x;
    10     char y;
    11     cout<<"请输入X的值:";
    12     cin>>x;
    13     overload(x);
    14     cout<<endl<<"请输入y的值:";
    15     cin>>y;
    16     overload(y);
    17     return 0;
    18 }
    19 
    20 void overload(int a)
    21 {
    22     cout<<"你输入的数值为:"<<a<<endl;
    23 }
    24 
    25 void overload(char b)
    26 {
    27     char i;
    28     i=b+1;
    29     cout<<"你输入的字符为:"<<i<<endl;
    30 }

     

    3、定义一个三角形类,用成员函数计算其周长、面积。

     1 #include<iostream>
     2 
     3 using namespace std;
     4 #include<math.h>
     5 
     6 class triangle
     7 {
     8     private:
     9     int a,b,c;
    10 
    11     public:
    12     triangle(int x=0,int y=0,int z=0);
    13     static void perimeter(triangle a);
    14     static void area(triangle b);
    15 };
    16 
    17 int main()
    18 {
    19     triangle  M(3,4,5);
    20     M.perimeter(M);
    21     M.area(M);
    22     return 0;
    23 }
    24 
    25 triangle::triangle(int x,int y,int z)
    26 {
    27     a=x;
    28     b=y;
    29     c=z;
    30 }
    31 
    32 void triangle::perimeter(triangle a)
    33 {
    34     int h;
    35     h=a.a+a.b+a.c;
    36     cout<<"三角形周长为:"<<h<<endl;
    37 }
    38 
    39 void triangle::area(triangle b)
    40 {
    41     float x,s;
    42     x=(b.a+b.b+b.c)/2;
    43     s=sqrt(x*(x-b.a)*(x-b.b)*(x-b.c));
    44     cout<<"三角形面积为:"<<s<<endl;
    45 }

     

    4、定义一个矩形类,用成员函数其周长、面积。

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class rectangle
     5 {
     6     private:
     7     int a,b;
     8     public:
     9     rectangle(int x=0,int y=0);
    10     void perimeter(rectangle i);
    11     void area(rectangle j);
    12 };
    13 
    14 int main()
    15 {
    16     rectangle ob1(3,5);
    17     ob1.perimeter(ob1);
    18     ob1.area(ob1);
    19     return 0;
    20 }
    21 
    22 rectangle::rectangle(int x,int y)
    23 {
    24     a=x;
    25     b=y;
    26 }
    27 
    28 void rectangle::perimeter(rectangle i)
    29 {
    30     int a;
    31     a=2*(i.a+i.b);
    32     cout<<"矩形的周长为:"<<a<<endl;
    33 }
    34 
    35 void rectangle::area(rectangle j)
    36 {
    37     int b;
    38     b=j.a*j.b;
    39     cout<<"矩形的面积为:"<<b<<endl;
    40 }

     

    5、定义一个圆类,用成员函数其周长、面积。

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class circle
     5 {
     6     private:
     7     int r;
     8     public:
     9     circle(int x);
    10     void perimeter(circle a);
    11     void area(circle b);
    12 };
    13 
    14 int main()
    15 {
    16     circle ob1(1);
    17     ob1.perimeter(ob1);
    18     ob1.area(ob1);
    19     return 0;
    20 }
    21 
    22 circle::circle(int a)
    23 {
    24     r=a;
    25 }
    26 
    27 void circle::perimeter(circle a)
    28 {
    29     float z;
    30     z=2*3.14*a.r;
    31     cout<<"圆的周长为:"<<z<<endl;
    32 }
    33 
    34 void circle::area(circle b)
    35 {
    36     float c;
    37     c=3.14*b.r*b.r;
    38     cout<<"圆的面积为:"<<c<<endl;
    39 }

     

  • 相关阅读:
    Appium(一):java环境、AndroidSDK环境
    SQL Server Merge语句的使用
    ASP.NET MVC下判断用户登录和授权的方法
    javascript的错误处理
    javascript的封装实例
    Javascript的封装
    ASP.NET MVC的请求生命周期
    Asp.Net页面生命周期
    SQL注入原理
    ASP.NET MVC:窗体身份验证及角色权限管理示例
  • 原文地址:https://www.cnblogs.com/xautlmx/p/3441386.html
Copyright © 2011-2022 走看看