#include <stdio.h> #include <iostream> #include <stdlib.h> using namespace std; class rectangle { private: double width,heigth;//˽ÓÐÊý¾Ý,Ö»Äܱ»ÄÚ²¿º¯Êýµ÷Óà public: rectangle():width(1.0),heigth(1.0){}//¹¹Ô캯Êý£¬È±Ê¡µÄ rectangle(int width,int heigth):width(width),heigth(heigth){}//¹¹Ô캯Êý double getarea()//ÐÐΪ { double sum=width*heigth; return sum; } double getperimeter()//ÐÐΪ { double c=(width+heigth)*2; return c; } ~rectangle()//Îö¹¹º¯Êý£¬³ÌÐò×Ô¶¯µ÷Ó㬾ßÌåʲôʱºò£¬¿ÉÒÔÉÏÍø²é { cout<<"end"<<endl; } }; int main() { double a,b; cin>>a>>b; rectangle rect(a,b); double sum=rect.getarea(); double c=rect.getperimeter(); printf("area=%lf ",sum); printf("perimeter=%lf ",c); return 0; } /* a=1,b=1; Êä³ö£º area=1.000000 perimeter=4.000000 end */
#include <stdio.h> #include <iostream> #include <stdlib.h> using namespace std; class rectangle { private: double width,heigth;//私有数据,只能被内部函数调用 public: rectangle();//只把函数名称先申明 rectangle(double a,double b); double getarea(); double getperimeter(); ~rectangle(); }; rectangle::rectangle()//具体函数,构造函数和析构函数最特别,没有返回值,且::前后都是类名 { width=1.0; heigth=1.0; } rectangle::rectangle(double a,double b)//构造函数的重载,每个函数都可以重载 { width=a; heigth=b; } double rectangle::getarea()//其他行为函数,返回值+类名+::+函数名,函数体与一般函数相同 { double sum=width*heigth; return sum; } double rectangle::getperimeter() { double c=(width+heigth)*2; return c; } rectangle::~rectangle()//析构函数 { cout<<"end"<<endl; } int main() { double a,b; cin>>a>>b; rectangle rect(a,b); double sum=rect.getarea(); double c=rect.getperimeter(); printf("area=%lf ",sum); printf("perimeter=%lf ",c); return 0; } /* a=1,b=1; 输出: area=1.000000 perimeter=4.000000 end */