C/C++函数类代码注释详解

C/C++代码注释详解

代码示例

在1个工程里面,包含以下关于C++函数定义及实现方法

项目Value
1
2构造函数和析构函数
3成员函数的重载
4访问修饰符
5友元函数
6静态成员变量和静态成员函数
7继承
8多态性
9静态成员
#include <iostream>
#include <stdio.h>
#include <cstdlib>		//用于支持string类型变量
using namespace std;    //使用std命名空间

//1.类的使用
class Shape {
private:                //private:不可以外部直接调用,需要通过内部函数进行访问修改    
    double length;
    double width;
	double radius;

    double calculateArea() {
        return length * width;
    }

public:
	double P_length;		//public函数可以直接访问或修改
	double P_width;
	double P_radius;
	static int count;
	//构造函数/静态成员变量和静态成员函数
    Shape(double r){
        radius = r;
		count++;
        cout << "构造函数被调用" << endl;
    }
    ~Shape(){
        cout << "析构函数被调用" << endl;
    }
    double getCircleArea(){
        return 3.14159 * radius * radius;
    }
	//普通函数
	void setDimensions(double len=9, double wid=10) {
        length = len;
        width = wid;
    }
    double getRectangleArea() {
        return length * width;
    }
	double getPublicArea(){
		return P_length*P_width;
	}
    //3.成员函数的重载
    int add(int a, int b) {
        return a + b;
    }

    double add(double a, double b) {
        return a + b;
    }
    
	//4.访问修饰符
    double getArea() {
        return calculateArea();
    }
	
	double getPArea(){
		return getPublicArea();
	}
	
	virtual double getArea8() {
        return 0.0;
    }
	
	/*友元函数*/
    friend double calculateArea5(Shape rect);		//注意:此处需要以类的名称定义rect
	
	/*静态成员变量*/
    static int getCount() {
        return count;
    }
	
};

//7.继承--------------
class ShapePrice:public Shape{
	public:
	ShapePrice(double r):Shape(r){
		
	}
	void speakprice(){
		cout << "ShapePrice" << endl;
	}
};

//5.友元函数
double calculateArea5(Shape rect) {
    return rect.length * rect.width;
};


class Rectangle8 : public Shape {
private:
    double length;
	double width;
public:
    Rectangle8(double len,double wid) : Shape(len) {	//必须建立基类函数的构造函数,如果基类没有构造函数,可以只实现派生函数的构造函数,不用继承基类的
        length = len;
		width = wid;
    }

    double getArea8() {
        return length*width;
    }
};

/*多态函数*/
class Circle8 : public Shape {
private:
    double radius;

public:
    Circle8(double r) : Shape(r) {		//必须建立基类函数的构造函数
        radius = r;
    }

    double getArea8() {
        return 3.14159 * radius * radius;
    }
};

int Shape::count = 0;  // 静态成员变量的初始化

int main() { 
    double area = 0; //面积值 
	cout << "-----------1.类的使用-------------" <<  endl;
    Shape shape(5.0); 
	cout << "使用函数默认参数9,10加载计算面积" << endl;
    shape.setDimensions();
    area = shape.getRectangleArea();
    cout << "矩形的面积是:" << area << endl;
	cout << "使用参数2,3加载计算面积" << endl;
	shape.setDimensions(2.0,3.0);
	area = shape.getRectangleArea();
	cout << "矩形的面积是:" << area << endl;

	cout << "----------2.构造函数和析构函数--------------"  << endl;
    area = shape.getCircleArea();
    cout << "圆的面积是:" << area << endl;

	cout << "----------3.成员函数的重载--------------" <<  endl;
    int result1 = shape.add(3, 4);
    double result2 = shape.add(2.5, 3.7);
    cout << "整数相加的结果:" << result1 << endl;  // 输出:整数相加的结果:7
    cout << "浮点数相加的结果:" << result2 << endl;  // 输出:浮点数相加的结果:6.2

	cout << "----------4.访问修饰符--------------" <<  endl;
    shape.setDimensions(5.0,3.0);
    shape.P_length = 6.0;
    shape.P_width = 4.0;
    area = shape.getArea();
    cout << "private:矩形的面积是:" << area << endl;
	area = shape.getPArea();
    cout << "public:矩形的面积是:" << area << endl;
	
	cout << "----------5.友元函数--------------" <<  endl;
	Shape rectangle5(5.0);
	rectangle5.setDimensions(4.0,5.0);
    area = calculateArea5(rectangle5);
    cout << "矩形的面积是:" << area << endl;
	
	cout << "----------6.静态成员变量和静态成员函数--------------" <<  endl;
	Shape circle1(5.0);
    Shape circle2(3.0);
	//int temp = circle1.getCount();
	//cout << "临时调试变量:圆的数量:" << temp << endl;  // 临时输出  结果与下面的一致
    int count = Shape::getCount();		//类名::函数名,或者定义类型:函数名的形式调用函数
    cout << "圆的数量:" << count << endl;  // 输出:圆的数量:3,该构造函数在shape(5.0)被调用过一次
	
	cout << "----------7.继承--------------" <<  endl;
	ShapePrice shapeprice(5);
	shapeprice.speakprice();
	
	cout << "----------8.多态性--------------" <<  endl;
	Rectangle8 rectangle8(5.0,3.0);
    Circle8 circle8(2.5);
    Shape* shape1 = &rectangle8;
    Shape* shape2 = &circle8;
    double area1 = shape1->getArea8();
    double area2 = shape2->getArea8();
    cout << "矩形的面积:" << area1 << endl;  // 输出:矩形的面积:15
    cout << "圆的面积:" << area2 << endl;    // 输出:圆的面积:19.6349

	cout << "----------9.静态成员--------------" <<  endl;
    cout << "静态成员count被累加的次数:" << Shape::getCount() << endl;  // 输出:静态成员累加次数:count的值
    while(1);
    return 0;
}

基于VC++6.0软件运行,结果如下
在这里插入图片描述

  • 6
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值