C++ 类的学习,创建

#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <iostream>
#include<string>
using namespace std;
class person{
    public:
        person():age(12),name("wangyueyue"){};//类的无参数构造函数  用来默认初始化列表成员
        person(int num,string namebuffer):age(num),name(namebuffer){};//有参数构造函数
       int age_num()const{return age;};
       string nameis()const{return name;};
       ~person(){};

    private:
       const int age;
       const string name;
};


#endif
#include "person.h"


int main()
{
    person mywife(13,"zsl");
    cout <<mywife.age_num() << endl;
    return 0;
}

在写的过程中出现了一个小问题

error: new types may not be defined in a return type

这是因为在类编写类的后面忘记添加“ ;”了

C++ primer中 指出,类的定义必须以分号结束。而且分号是必须的,因为类定义之后可以接一个对象定义列表。

class Sales_item{/*............*/};
class Sales_item{/*............*/}accum,trans;


1. C++变量的初始化方式

首先把需要初始化的成员变量分为几类:

Ø  一般变量(int)

Ø  静态成员变量(static int)

Ø  常量(const int )

Ø  静态常量(static const int)

 对应的初始化方式是:

Ÿ  一般变量可以在初始化列表里或者构造函数里初始化不能直接初始化或者类外初始化

Ÿ  静态成员变量必须在类外初始化

Ÿ  常量必须在初始化列表里初始化

Ÿ  静态常量必须只能在定义的时候初始化(定义时直接初始化)

 

举一个简单的例子

  1. #include <iostream>    
  2. #include <string>    
  3. using namespace std;   
  4. class Test   
  5. {   
  6. private:   
  7.     int a;   
  8.     static int b;   
  9.     const int c;   
  10.     static const int d=4;   
  11. public:   
  12.     Test():c(3)     //a(1)或者在初始化列表里初始化    
  13.     {   
  14.         a=1;   
  15.     }   
  16. };   
  17. int Test::b=2;  
  18. void main()   
  19. {   
  20.     Test t;   
  21. }  
#include <iostream> 
#include <string> 
using namespace std; 
class Test 
{ 
private: 
    int a; 
    static int b; 
    const int c; 
    static const int d=4; 
public: 
    Test():c(3)     //a(1)或者在初始化列表里初始化 
    { 
        a=1; 
    } 
}; 
int Test::b=2;
void main() 
{ 
    Test t; 
}

 

 

2. 类成员变量初始化顺序

C++

有如下几条:

1构造函数初始化列表的变量优先于构造函数(至少明显的写在前面)  (若都在初始化列表中初始化,则按声明顺序初始化,与初始化列表中的顺序无关)

2静态成员变量先于实例变量

3父类成员变量先于子类成员变量

4父类构造函数先于子类构造函数

 举一个例子:

  1. #include <iostream>   
  2. #include <string>   
  3. using namespace std;  
  4. class Test  
  5. {  
  6. public:  
  7.     Test(string n)  
  8.     {  
  9.         cout<<n<<endl;  
  10.     }  
  11. };  
  12. class Base  
  13. {  
  14. public:  
  15.     static Test* a;  
  16.     Test* b;  
  17.     Test* c;  
  18.     Base():b(new Test("b"))  
  19.     {  
  20.         c=new Test("c");  
  21.     }  
  22.     virtual ~Base()  
  23.     {  
  24.         if(a) delete a;//似乎是很欠妥的做法   
  25.         if(b) delete b;  
  26.         if(c) delete c;  
  27.     }  
  28. };  
  29. Test* Base::a=new Test("a");  
  30. class Derived:Base  
  31. {  
  32. public:  
  33.     static Test* da;  
  34.     Test* db;  
  35.     Test* dc;  
  36.     Derived():db(new Test("db"))  
  37.     {  
  38.         dc=new Test("dc");  
  39.     }  
  40.     ~Derived()  
  41.     {  
  42.         if(da) delete da;//似乎是很欠妥的做法   
  43.         if(db) delete db;  
  44.         if(dc) delete dc;  
  45.     }  
  46. };  
  47. Test* Derived::da=new Test("da");  
  48.   
  49. void main()  
  50. {  
  51.     Derived d;  
  52. }  
#include <iostream>
#include <string>
using namespace std;
class Test
{
public:
	Test(string n)
	{
		cout<<n<<endl;
	}
};
class Base
{
public:
	static Test* a;
	Test* b;
	Test* c;
	Base():b(new Test("b"))
	{
		c=new Test("c");
	}
	virtual ~Base()
	{
		if(a) delete a;//似乎是很欠妥的做法
		if(b) delete b;
		if(c) delete c;
	}
};
Test* Base::a=new Test("a");
class Derived:Base
{
public:
	static Test* da;
	Test* db;
	Test* dc;
	Derived():db(new Test("db"))
	{
		dc=new Test("dc");
	}
	~Derived()
	{
		if(da) delete da;//似乎是很欠妥的做法
		if(db) delete db;
		if(dc) delete dc;
	}
};
Test* Derived::da=new Test("da");

void main()
{
	Derived d;
}

结果是:

a
da

b

c

db

dc


 

java和C#语言

1   类成员变量初始化先于类的构造函数

2   静态成员变量先于实例变量

3   父类成员变量先于子类成员变量 (C#相反)

4   父类构造函数先于子类构造函数

举一个java的例子:

  1. class Base  
  2. {  
  3.     public static Test a=new Test("a");  
  4.     public static Test b;  
  5.     public Test c=new Test("c");  
  6.     public Test d;  
  7.     static  
  8.     {  
  9.         b=new Test("b");  
  10.     }  
  11.     public Base()  
  12.     {  
  13.         d=new Test("d");  
  14.     }  
  15.     public static void main(String[] args) {  
  16.         new Derived();  
  17.     }  
  18. }  
  19. class Derived extends Base  
  20. {  
  21.     public static Test da=new Test("da");  
  22.     public static Test db;  
  23.     public Test dc=new Test("dc");  
  24.     public Test dd;  
  25.     static  
  26.     {  
  27.         db=new Test("db");  
  28.     }  
  29.     public Derived()  
  30.     {  
  31.         dd=new Test("dd");  
  32.     }  
  33. }  
  34.   
  35. class Test  
  36. {  
  37.     public Test (String name) {  
  38.         System.out.println(name);  
  39.     }  
  40. }  
class Base
{
	public static Test a=new Test("a");
	public static Test b;
	public Test c=new Test("c");
	public Test d;
	static
	{
		b=new Test("b");
	}
	public Base()
	{
		d=new Test("d");
	}
	public static void main(String[] args) {
		new Derived();
	}
}
class Derived extends Base
{
	public static Test da=new Test("da");
	public static Test db;
	public Test dc=new Test("dc");
	public Test dd;
	static
	{
		db=new Test("db");
	}
	public Derived()
	{
		dd=new Test("dd");
	}
}

class Test
{
	public Test (String name) {
		System.out.println(name);
	}
}

运行结果是:

a

b

da

db

c

d

dc

dd




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值