面向对象方法程序实例(字符串为函数参数的注意事项)

友元和运算符重载

题设

在这里插入图片描述

类体

在这里插入图片描述

C++代码实现

Student.h文件

class Student
{
    
    
private:
    char *m_pcSno;
    char *m_pcSname;
    int m_nScore;
    //how to store the string ?
public:
    Student(char *pcSno,char *pcSname,int nScore);
    ~Student();
    Student& operator=(int Score);
    friend void display(const Student &stu);
};

Student.cpp文件

#include <iostream>
#include <string.h>
#include "Student.h"

using namespace std;

Student::Student(char *pcSno,char *pcSname,int nScore)
{
    
    
    // the stirng is stored in the static area,
    //so dont need to create an area to store the string
    m_pcSname = new char[strlen(pcSname) + 1];
    strcpy(m_pcSname,pcSname);
    m_pcSno = new char[strlen(pcSno) + 1];
    strcpy(m_pcSno,pcSno);
    m_nScore = nScore;
}

Student::~Student()
{
    
    
    delete []m_pcSname;
    delete []m_pcSno;
    cout<<"deleting"<<endl;
}

Student& Student::operator=(int Score)
{
    
    
    m_nScore = Score;
    return *this;
}
  • main函数测试文件
#include <iostream>
#include "Student.h"

using namespace std;

int main()
{
    
    
    Student s("YC1710011","zhangninig",90);
    display(s);
    s = 93;
    display(s);
    return 0;
}
void display(const Student &stu)
{
    
    
    cout<<"name:"<<stu.m_pcSname<<endl;
    cout<<"no:"<<stu.m_pcSno<<endl;
    cout<<"score:"<<stu.m_nScore<<endl;
}

分析总结

  • 注意,对于字符串的处理,传入的是字符串,在函数体内部,要申请特定长度的空间用来存储字符串,同时长度要在原长上加1
Student::Student(char *pcSno,char *pcSname,int nScore)
{
    
    
    m_pcSname = new char[strlen(pcSname) + 1];
    //比原长要长一个单位,保存‘\0’
    strcpy(m_pcSname,pcSname);
    //采用strcpy函数来进行复制
    m_pcSno = new char[strlen(pcSno) + 1];
    strcpy(m_pcSno,pcSno);
    m_nScore = nScore;
}

静态成员和多文件结构

题设

在这里插入图片描述

C++代码实现

Cube.h文件

class Cube
{
    
    
private:
    double side;
    static int totalNumber;
public:
    Cube();
    Cube(double s);
    ~Cube();
    double getVolume();
    int getTotalNumber();
    void  SetSide(double s);
};

Cube.cpp文件

#include <iostream>
#include "Cube.h"

using namespace std;

int Cube::totalNumber = 0;

Cube::Cube()
{
    
    
    totalNumber ++;
    side = 0;
}

Cube::Cube(double s)
{
    
    
    totalNumber ++;
    side = s;
}

Cube::~Cube()
{
    
    
    totalNumber --;
    cout<<"deleting!"<<endl;
}

double Cube::getVolume()
{
    
    
    return side * side * side;
}

int Cube::getTotalNumber()
{
    
    
    cout<<"the total number of the cube is "<<totalNumber<<endl;
}

void Cube::SetSide(double s)
{
    
    
    side = s;
}

main测试文件

#include <iostream>
#include "Cube.h"

using namespace std;

int main()
{
    
    
    Cube c1(4),c2;
    cout<<c1.getVolume()<<endl;
    c2.SetSide(7);
    cout<<c2.getVolume()<<endl;
    c2.getTotalNumber();
    return 0;
 }

分析与总结

  • 对于totalNumber的静态变量,用来统计的整个类体的对象的数目的,在对象的构造函数中要存在,同样的在析构函数也要存在,确保数据动态变化。
Cube::Cube()
{
    
    
    totalNumber ++;
    //及时更新的静态数据
    side = 0;
}

Cube::Cube(double s)
{
    
    
    totalNumber ++;
    //及时更新动态数据
    side = s;
}

Cube::~Cube()
{
    
    
    totalNumber --;
    //及时更新动态数据
    cout<<"deleting!"<<endl;
}
  • 关于静态数据成员的使用
    • 在类体内部进行声明,需要关键字static
    • 在类体外定义,不需要用static关键字

对象成员

题设

在这里插入图片描述

类体描述

Point类
在这里插入图片描述
Circle类
在这里插入图片描述

C++实现

DefineClass.h文件

class Point1
{
    
    
private:
    int m_nX;
    int m_nY;
public:
    Point1 (int nX,int nY);
    ~Point1();
    friend class Circle;
};


class Circle
{
    
    
private:
    Point1 m_pointCenter;
    int m_nR;
public:
    Circle(int nX,int nY,int nR);
    ~Circle();
    void displayInfo();
};

DefineClass.cpp文件

Point1::~Point1()
{
    
    
    cout<<"ddeleting the point!"<<endl;
}

Circle::Circle(int nX,int nY,int nR):m_pointCenter(nX,nY)
{
    
    
    m_nR = nR;
    cout<<"constructing the circle"<<endl;
    
}
//how to organize the member of the object

Circle::~Circle()
{
    
    
    cout<<"deleting the circle!"<<endl;
}

void Circle::displayInfo()
{
    
    
    cout<<"the cneter is :"<<m_pointCenter.m_nX<<"  "
        <<m_pointCenter.m_nY<<endl;
    cout<<"the height is :"<<m_nR<<endl;
}

main测试文件

#include <iostream>
#include "DefineClass.h"

using namespace std;

int main()
{
    
    
    Circle c(10,15,5);
    c.displayInfo();
    return 0;
}

分析与总结

  • 对象的成员的初始化方式,直接在参数声明列表的后续进行的声明,在函数体中不需要再另外说明
Circle::Circle(int nX,int nY,int nR):m_pointCenter(nX,nY)
{
    
    
    m_nR = nR;
    cout<<"constructing the circle"<<endl;  
}
  • 作为类的友好类,可以访问另一类中的所有成员函数,包括私有成员。A可以访问B中的所有的资源

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Blackoutdragon/article/details/108823884
今日推荐