Chinaunix首页 | 论坛 | 博客
  • 博客访问: 160456
  • 博文数量: 83
  • 博客积分: 3956
  • 博客等级: 中校
  • 技术积分: 663
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-24 16:29
文章分类

全部博文(83)

文章存档

2010年(83)

我的朋友

分类: C/C++

2010-10-22 20:04:05

1.类与对象概念


    在定义类时,只是定义了一种数据类型,即说明程序中可能会出现该类型的数据,并不为类分配存储空间。只有在定义了属于类的变量后,系统才会为类的变量分配空间。对象是类的实例,定义对象之前,一定要先说明该对象的类。

    不同对象占据内存中的不同区域,它们所保存的数据各不相同,但对成员数据进行操作的成员函数的程序代码均是一样的。

对象的定义格式:

    《存储类型》类名   对象1,对象2《,......》;

    在建立对象时,只为对象分配用于保存数据成员的内存空间,而成员函数的代码为该类的每一个对象所共享。

    对象的定义方法同结构体定义变量的方法一样,也分三种,当类中有数据成员的访问权限为私有时,不允许对对象进行初始化。


对象的使用


    一个对象的成员就是该对象的类所定义的成员,有成员数据和成员函数,引用时同结构体变量类似,用“.”运算符。


#include <iostream>

using namespace std;

class A {
    float x, y;

public:
    float m, n;
    void Setxy(float a, float b);
    void Print(void);
};

void A::Setxy(float a, float b)
{
    x = a;
    y = b;
}

void A::Print(void)
{
    cout << x << '\t' << y << endl;
}

int main(void)
{
    A a1, a2;    //定义对象

    a1.m = 10; a1.n = 20;    //为公有成员数据赋值

    a1.Setxy(2.0, 5.0);    //为私有成员数据赋值

    a1.Print();
}


    用成员选择运算符“.”只能访问对象的公有成员,而不能访问对象的私有成员或保护成员。若要访问对象的私有的数据成员,只能通过对象的公有成员函数来获取。


#include <iostream>

using namespace std;

class A {
    float x, y;
public:
    float m, n;
    void Setxy(float a, float b) {x = a; y = b;}
    void Print(void) { cout << x << '\t' << y << endl;}
};

int main(void)
{
    A a1, a2;
    a1.m = 10; a1.n = 20; //为公有成员数据赋值

//    a1.x = 2; a1.y = 5; //非法,私有成员不能在类外访问

    a1.Setxy(2.0, 5.0);
    a1.Print();
}

    同类型的对象之间可以整体赋值,这种赋值与对象的成员的访问权限无关。

#include <iostream>

using namespace std;

class A {
    float x, y;

public:
    float m, n;
    void Setxy(float a, float b) {x = a; y = b;}
    void Print(void){ cout << x << '\t' << y <<endl;}
};

int main(void)
{
    A a1, a2;
    a1.m = 10; a1.n = 20;
    a1.Setxy(2.0, 5.0);
    a2 = a1;
    a1.Print();
    a2.Print();
}

    对象可以作函数的入口参数(实参、形参),也可以作函数的出口参数。这与一般变量作为函数的参数是完全相同的。可以定义类类型的指针,类类型的引用,对象数组,指向类类型的指针数组和指向一维或多维数组的指针变量。一个类的对象,可作为另一个类的成员。

2.类作用域、类类型的作用域和对象的作用域

    类体的区域称为类作用域。类的成员函数与成员数据,其作用域都是属于类的作用域,仅在该类的范围内有效,故不能在主函数中直接通过函数名和成员名来调用函数。

    类类型的作用域:在函数定义之外定义的类,其类名的作用域为文件作用域;而在函数体内定义的类,其类名的作用域为块作用域 。对象的作用域与前面介绍的变量作用域完全相同 , 全局对象、局部对象、局部静态对象等。

class A {
        float x,y;
public:
       float m,n;
       void Setxy( float a, float b ){ x=a; y=b; }
       void Print(void) { cout<<x<<‘\t’<<y<<endl; }
}a3,a4;
void main(void)
{ A a1,a2;
      class B{
                 int i,j;
      public :
             void Setij(int m, int n){ i=m; j=n; }
        };
      B b1,b2;
      a1.Setxy(2.0, 5.0); b1.Setij(1,2);
}

    在定义一个类时, 在其类体中又包含了一个类的完整定义,称为类的嵌套 。

    类是允许嵌套定义的 。

class A {
        class B{
                 int i,j;
      public :
             void Setij(int m, int n){ i=m; j=n; }
        };
        float x,y;
public:
       B b1,b2;
       void Setxy( float a, float b ){ x=a; y=b; }
       void Print(void) { cout<<x<<‘\t’<<y<<endl; }
};

类的对象如何引用私有数据成员

1、通过公有函数为私有成员赋值

class Test{
    int x , y;
    public:
       void Setxy(int a, int b){x=a;    y=b;}
       void Printxy(void) {cout<<"x="<<x<<'\t'<<"y="<<y<<endl;}
} ;
void main(void)
{     Test p1,p2;
        p1.Setxy(3, 5);        
       p1.Printxy( );
}

2、利用指针访问私有数据成员

class Test{
        int x,y;
   public:
       void Setxy(int a, int b) {x=a;    y=b;}
       void Getxy(int *px, int *py) {*px=x;*py=y;} //提取x,y值

       void Printxy(void){cout<<"x="<<x<<'\t'<<"y="<<y<<endl; }
};
void main(void)
{ Test p1,p2;
      p1.Setxy(3,5);
      int a, b;
      p1.Getxy(&a, &b);//将 a=x, b=y

      cout<<a<<'\t'<<b<<endl;
}

3、利用函数访问私有数据成员

class Test{
        int x,y;
   public:
       void Setxy(int a, int b) {x=a;    y=b;}
       int Getx(void) { return x;} //返回x值

       int Gety(void) { return y;} //返回y值

       void Printxy(void){cout<<"x="<<x<<'\t'<<"y="<<y<<endl; }
};
void main(void)
{ Test p1,p2;
      p1.Setxy(3,5);
      int a,b;
      a=p1.Getx( ); b=p1.Gety(); //将 a=x, b=y

      cout<<a<<'\t'<<b<<endl;
}

#include <iostream>

using namespace std;

class Test {
    int x,y;
public:
    void Setxy(int a, int b);
    int Getx(void);
    int Gety(void);
    void Print(void);
};

void Test::Setxy(int a, int b)
{
    x = a;
    y = b;
}

int Test::Getx(void)
{
    return x;
}

int Test::Gety(void)
{
    return y;
}

void Test::Print(void)
{
    cout << "x=" << x << '\t' << "y=" << y << endl;
}

int main(void)
{
    Test p1, p2;
    p1.Setxy(3, 5);
    int a,b;
    a = p1.Getx(); b = p1.Gety();
    cout << a << '\t' << b << endl;
}

4、利用引用访问私有数据成员

class Test{
    int x,y;
   public:
       void Setxy(int a, int b){    x=a; y=b;}
      void Getxy(int &px, int &py) { px=x;    py=y;    } //提取x,y值

      void Printxy(void){cout<<"x="<<x<<'\t'<<"y="<<y<<endl; }
};
void main(void)
{ Test p1,p2;
       p1.Setxy(3,5);
       int a,b;
       p1.Getxy(a, b);//将 a=x, b=y

       cout<<a<<'\t'<<b<<endl;
}

类引用举例(三角形类:三角形的三边及与三边相关的运算)

#include <iostream>
#include <cmath>
using namespace std;

class Triangle {
private:
    float a,b, c;    //三边为私有成员数据

public:
    void Setabc(float x, float y, float z);
    void Getabc(float &x, float &y, float &z);
    float Perimeter(void);    //计算三角形周长

    float Area(void);
    void Print(void);
};

void Triangle::Setabc(float x, float y, float z)
{
    a = x;
    b = y;
    c = z;
}

void Triangle::Getabc(float &x, float &y, float &z)
{
    x = a;
    y = b;
    z = c;
}

float Triangle::Perimeter(void)
{
    return (a+b+c)/2;
}

float Triangle::Area(void)
{
    float area, p;
    p = Perimeter();
    area = sqrt((p-a)*(p-b)*(p-c)*p);
    return area;
}

void Triangle::Print(void)
{
    cout<<"Peri="<<Perimeter()<<'\t'<<"Area="<<Area()<<endl;
}

int main(void)
{
    Triangle Tril;
    Tril.Setabc(4,5,6);
    float x,y,z;
    Tril.Getabc(x, y, z);
    cout<<x<<'\t'<<y<<'\t'<<z<<endl;
    cout<<"s="<<Tril.Perimeter()<<endl;
    cout<<"Area="<<Tril.Area()<<endl;
    cout<<"Tril:"<<endl;
    Tril.Print();
}

运行结果:

4    5    6

s=7.5

Area=9.92157

Tril:

Peri=7.5    Area=9.92157

阅读(616) | 评论(0) | 转发(0) |
0

上一篇:C++ 引用

下一篇:类与对象 (二)

给主人留下些什么吧!~~