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软件运行,结果如下