#include <string.h>
#include <iostream>
using namespace std;
class stu
{
private: //定义数据成员
int num;
static int num1;//1.定义静态变量
char name[10];
char sex;
public: //声明,成员函数,初始化构造函数。。。
int pub_test=1;
stu(int n,char nam[],char s) //带参数的构造函数可以给数据成员初始化,因为class 不占储存空间,不能直接初始化,类外也可以定义构造函数| stu :: stu()
{
num = n;
strcpy(name,nam);
sex = s;
}
~stu()
{
cout<<"stud has been destructed!"<<endl;
}
void display(); //must be define !!!
static int numadd();
// void display() //class in
// {
// cout<<"num:"<<num<<endl;
// cout<<"name:"<<name<<endl;
// cout<<"sex:"<<sex<<endl;
// }
};
class stu2
{
private: //定义数据成员
int num;
char name[10];
char sex;
public: //声明,成员函数,初始化构造函数。。。
int pub_test=1;
stu2(int n,char nam[],char s) : num(n),sex(s) //带参数的构造函数可以给数据成员初始化,因为class 不占储存空间,不能直接初始化,类外也可以定义构造函数| stu :: stu()
{
// num = n;
strcpy(name,nam);
// sex = s;
}
~stu2()
{
cout<<"stud has been destructed!"<<endl;
}
void display2()const //常成员函数
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
};
//2.静态成员函数
int stu::numadd()
{
num=num+1; //非法的,只能用静态变量
num1=num1+1;//static int num1
cout<<"static numadd:"<<num1<<endl;
}
int stu::num1=0; //3.赋初值 ::
void stu:: display() //class out
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
cout<< "class stud = "<<sizeof(stu)<<endl;
cout<< "class stud = "<<sizeof(sex)<<endl;
cout<< "class stud = "<<sizeof(num)<<endl;
cout<< "class stud = "<<sizeof(name)<<endl;
cout<< "class public pub_test = "<<pub_test<<endl;
}
int main(int argc, char *argv[])
{
stu std1(100,"S_yang",'m'),std2(101,"quan",'w');//1.class name call
std1.display();
std2.display();
stu s(111,"P_yang",'p'),*p; //2.point call
p = &s;
p->pub_test=55; //public
// p->name="P_yang"; //error can't call private
// p->num="111";//error
// p->sex='p';//error
p->display();
stu c(222,"C_yang",'C'); //3.& call
stu &c1 = c;
c1.display();
stu2 const ct(555,"CT_yang",'c'); //常对象需要访问成员函数,要把成员函数也声明成const
ct.display2();
/**常对象、指针**/
stu cp(666,"CP_yang",'c'),cp1(667,"CP_yang2",'c');
stu *const cw=&cp;
cw->display();
//cw = &cp; //cw指向对象tl,此后不能改变指向
//cw = &cp1;//error,不能改变指向
/**对象的动态建立**/
stu *n = new stu(777,"NEW_yang",'c');
n->display();
delete n;
/**对象赋值**/
stu cpy(cp);//cpy 是复制cp的对象
cpy = c; //对象赋值
cpy.display();
/**静态成员函数**/
stu::numadd(); //4.静态成员函数调用
return 0;
}
结果:
num:100
name:S_yang
sex:m
class stud = 20
class stud = 1
class stud = 4
class stud = 10
class public pub_test = 1
num:101
name:quan
sex:w
class stud = 20
class stud = 1
class stud = 4
class stud = 10
class public pub_test = 1
num:111
name:P_yang
sex:p
class stud = 20
class stud = 1
class stud = 4
class stud = 10
class public pub_test = 55
num:222
name:C_yang
sex:C
class stud = 20
class stud = 1
class stud = 4
class stud = 10
class public pub_test = 1
num:555
name:CT_yang
sex:c
num:666
name:CP_yang
sex:c
class stud = 20
class stud = 1
class stud = 4
class stud = 10
class public pub_test = 1
num:777
name:NEW_yang
sex:c
class stud = 20
class stud = 1
class stud = 4
class stud = 10
class public pub_test = 1
stud has been destructed!
num:222
name:C_yang
sex:C
class stud = 20
class stud = 1
class stud = 4
class stud = 10
class public pub_test = 1
static numadd:1
stud has been destructed!
stud has been destructed!
stud has been destructed!
stud has been destructed!
stud has been destructed!
stud has been destructed!
stud has been destructed!
stud has been destructed!
C++ 基本概念 编程练习
最新推荐文章于 2024-10-19 18:12:33 发布