box.h
#ifndef BOX_H
#define BOX_H
#include<iostream>
#include<cstring>
#include<malloc.h>
using namespace std;
class Box
{
public:
Box();
Box(int a, int b, int c, char *name, char str);
Box(const Box&p);
void Surface_area();
void box_v();
void setval(int a, int b, int c, char *name, char str,int num2);
void printf_name();
void printf_str();
void printf_val();
~Box();
static void printf_str_1()
{
num = 10;
num2 = 60;
cout << "内部静态函数给静态成员(公有、私有)赋值" << endl;
cout << num << endl;
cout << num2 << endl;
}
void test00() const
{
m = 100;
num = 111;
num2 = 222;
cout << "类内_mutable修饰 m= " << m << endl;
cout << "类内_x= " << x << endl;
cout << "类内_w= " << w << endl;
cout << "类内_n= " << n << endl;
cout << "类内_未被mutable修饰的静态公有静态成员:num= " << num << endl;
cout << "类内_未被mutable修饰的静态私有静态成员:num2= " << num2 << endl;
cout << "类内_name_1= " << name_1 << endl;
cout << "类内_h= " << h << endl;
}
static int num;
int n;
mutable int m;
private:
int x;
int w;
int *h;
char *name_1;
char str;
static int num2;
};
#endif
box.cpp
#include "box.h"
int Box::num = 10;
int Box::num2 = 20;
Box::Box()
{
cout<<"默认构造!!!"<<endl;
x = 0;
w = 0;
h = new int;
str = NULL;
name_1 = new char[40];
n = 0;
}
Box::Box(int a, int b, int c, char *name, char str)
{
cout<<"有参构造"<<endl;
this->x = a;
this->w = b;
this->h = new int(c);
this->str = str;
this->name_1 = new char[40];
strcpy(this->name_1, name);
}
Box::Box(const Box &p)
{
this->x = p.x;
this->h = new int (*p.h);
this->w = p.w;
this->str = p.str;
this->name_1 = new char[20];
strcpy(this->name_1, p.name_1);
}
void Box::Surface_area()
{
int i = 2 * ((x * w ) + (w * (*h)) + (x * (*h)));
cout<< "表面积是:"<< i << endl;
}
void Box::box_v()
{
int i = x * w * (*h) ;
cout<< "该立方体的体积为:"<< i << endl;
}
void Box::setval(int a, int b, int c, char *name, char str, int num2)
{
this->x = a;
this->w = b;
this->h = new int(c);
this->str = str;
Box::num2 = num2;
this->name_1 = new char[20];
strcpy(this->name_1, name);
}
void Box::printf_name()
{
cout << "长方体名称:"<< name_1 << endl;
}
void Box::printf_str()
{
cout << "长方体编号:" << str << endl;
}
void Box::printf_val()
{
cout << (*h) <<" "<< w <<" "<< x <<" "<< name_1 <<" "<< str \
<< num2<< endl;
}
Box::~Box()
{
cout<<"析构函数释放~"<< endl;
if(h != NULL)
{
delete h;
}
if(name_1 != NULL)
{
delete[]name_1;
}
}
main.cpp
#include <iostream>
#include"box.h"
using namespace std;
int main()
{
#if 0
cout << "输出rec2的信息-赋值+打印:" << endl;
Box rec;
rec.setval(10, 5, 6,"box", 'a',223);
cout << "地址:" << &rec << endl;
rec.printf_val();
cout << "输出rec2的信息-赋值+打印:" << endl;
Box rec2;
rec2.setval(1, 2, 3,"dd", 'c',23);
cout << "地址:" << &rec2 << endl;
rec2.printf_val();
putchar('\n');
#endif
#if 0
cout << "类内公有静态成员属性-赋值+打印:" << endl;
Box::num = 220;
cout << Box::num << endl;
putchar('\n');
#endif
#if 0
cout << "类内公有静态成员函数-打印:" << endl;
Box::printf_str_1();
putchar('\n');
#endif
#if 1
Box rec3;
cout << "常函数信息:" << endl;
rec3.test00();
putchar('\n');
#endif
#if 0
const Box rec4;
cout << "类外_const修饰的常对象.赋值+打印:" << endl;
rec4.m = 2;
rec4.num = 333;
cout << "m = " << rec4.m << endl;
putchar('\n');
#endif
return 0;
}
图示:
|