写一个程序定义抽象基类Cshape。帮忙做道程序题。谢谢

写一个程序定义抽象基类Cshape。帮忙做道程序题。谢谢,第1张

#include <iostream>

using namespace stdclass Cshape

{

public:

virtual double GetArea() = 0

}class Ccircle:public Cshape

{

public:

Ccircle(double r):m_r(r) {}

virtual double GetArea()

{

return (3.14 *m_r * m_r)

}

private:

double m_r

}class Csquare:public Cshape

{

public:

Csquare(double a):m_a(a) {}

virtual double GetArea()

{

return (m_a * m_a)

}

private:

double m_a

}class Crectangle:public Cshape

{

public:

Crectangle(double a, double b):m_a(a), m_b(b) {}

virtual double GetArea()

{

return (m_a * m_b)

}

private:

double m_a

double m_b

}class Ctrapeziod:public Cshape

{

public:

Ctrapeziod(double a, double b, double h):m_a(a), m_b(b), m_h(h) {}

virtual double GetArea()

{

return ((m_a + m_b) * m_h / 2)

}

private:

double m_a

double m_b

double m_h

}class Ctriangle:public Cshape

{

public:

Ctriangle(double a, double h):m_a(a), m_h(h) {}

virtual double GetArea()

{

return (m_a * m_h / 2)

}

private:

double m_h

double m_a

}int main()

{

Cshape* pShape[5]

pShape[0] = new Ccircle(5)

pShape[1] = new Ctriangle(3.4, 8.6)

pShape[2] = new Csquare(4.6)

pShape[3] = new Ctrapeziod(4.5, 7.3, 4)

pShape[4] = new Crectangle(5, 6)

cout<<"圆形面积:"<<pShape[0]->GetArea()<<endl

cout<<"三角形面积:"<<pShape[1]->GetArea()<<endl

cout<<"正方形面积:"<<pShape[2]->GetArea()<<endl

cout<<"梯形面积:"<<pShape[3]->GetArea()<<endl

cout<<"长方形面积:"<<pShape[4]->GetArea()<<endl

double sum = 0

cout<<"它们面积总和为:"

for (int i=0i!=5++i)

{

sum += pShape[i]->GetArea()

delete pShape[i]

pShape[i] = NULL

}

cout<<sum<<endl

return 0

}

#include <stdio.h>

int main (void) {

float r,pi,area

pi = 3.14159

printf("请输入圆的半径r:")

scanf("%f",&r)

putchar ('\n') 

area = pi*r*r

printf ("半径为%.2f的圆,其面积为:%.2f\n",r,area)

getch () 

    return 0

}

运行结果

我写了个样例,你看一下,先定义Student

  public class Student

    {

        public string Name { get set }

        private DateTime _birthday

        public DateTime Birthday

        {

            get { return _birthday }

            set { _birthday = value }

        }

        public int Age

        {

            get { return DateTime.Now.Year - Birthday.Year + 1 }

        }

        /// <summary>

        /// 构造函数

        /// </summary>

        /// <param name="name"></param>

        /// <param name="birthday"></param>

        public Student(string name, string birthday)

        {

            this.Name = name

            this.Birthday = Convert.ToDateTime(birthday)

        }

        /// <summary>

        /// 析构函数

        /// </summary>

        ~Student()

        {

            Console.WriteLine("~Student()析构函数")

        }

    }

调用:

 static void Main(string[] args)

        {

            Console.WriteLine("****************************")

            {

                Dosomething()

            }

            GC.Collect()

            Console.WriteLine("****************************")

            Console.ReadKey()

        }

        static void Dosomething()

        {

            Student student = new Student("赵兰新", "1992-01-01")

            Console.WriteLine("姓名:{0},出生日期:{1},年龄:{2}", student.Name, student.Birthday.ToString("yyyy-MM-dd"), student.Age)

        }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11071827.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存