电商专业学习嵌入式软件开发第五十七天

  • C++第三天

今天主要讲了class和string以及构造函数和析构函数。最后老师带着我们完成了一个简单的游戏程序。老师留的作业还是不能正确的使用代码表达出来,最多只能将伪代码写出来。因为基础薄弱,今天已经正式向代理班主任曹老师申请到新班重修C基础,等新班正式开办就过去。

class:面向对象的类,可以把class看作struct

#include <iostream>
#include <string>
using namespace std;
//struct Student  //可以把class看作struct
class Student
{
private: //不能通过对象直接访问
    //可以通过共有接口间接访问
    string m_strId;
    string m_strName;
    float m_fScore;
public:  //通过对象直接访问
    string m_strFace;
    //成员函数
    void init(string id, string name, float score = 60.0f, string face = "cool");
    void info();
#if 0   
    //在类中定义的普通成员函数默认为inline函数
    void init(string id, string name, float score = 60.0f, string face = "cool")
    {
        m_strId = id;
        m_strName = name;
        m_fScore = score;
        m_strFace = face;
    }
    void info()
    {
        cout << "id:" << m_strId << " name:" << m_strName << " score:" << m_fScore << " face:" << m_strFace << endl;
    }
#endif
};
void Student::init(string id, string name, float score, string face)
{
    m_strId = id;
    m_strName = name;
    m_fScore = score;
    m_strFace = face;
}
void Student::info()
{
    cout << "id:" << m_strId << " name:" << m_strName << " score:" << m_fScore << " face:" << m_strFace << endl;
}
int main(void)
{
    Student stu;
    stu.m_strFace = "cool";
    cout << stu.m_strFace << endl;  

//  stu.strName = "刘德华";  // X
//  cout << stu.strName << endl;  // X

    stu.init("10101010", "刘德华", 101, "very cool");
    stu.info();

    return 0;
}

构造函数:

#include <iostream>
#include <string>
using namespace std;
class Student
{
private: //不能通过对象直接访问
    //可以通过共有接口间接访问
    string m_strId;
    string m_strName;
    float m_fScore;
public:  //通过对象直接访问
    //如果没有自定义构造函数则会自动生成一个无参的构造函数
    //假如自定义了有参构造函数则不会自动生成一个无参的构造函数
    //要想使用无参构造函数则必须自定义一个无参的构造函数
    //构造函数,在生成对象的时候自动调用,用来构造并初始化对象
#if 1
    Student()
    {
        cout << "Student()\n";
    }
#endif
    Student(string id, string name, float score = 60.0f, string face = "cool")
    {
        m_strId = id;
        m_strName = name;
        m_fScore = score;
        m_strFace = face;
        cout << "Student(...)\n";   
    }
    string m_strFace;
    void info()
    {
        cout << "id:" << m_strId << " name:" << m_strName << " score:" << m_fScore << " face:" << m_strFace << endl;
    }
};
int main(void)
{
    //如果对象后面没有实参则默认调用无参构造函数
    //Student stu;
    //如果对象后面有实参则调用参数匹配的构造函数
    //Student stu("1001", "刘德华", 89, "cool");
    //Student *pstu = new Student[32];
    //Student *p = (Student*)malloc(sizeof(Student));
    //stu.info();

    Student *p1 = new Student;
    Student *p2 = new Student("1001", "刘德华", 89, "cool");
    p1->info();
    p2->info();

    int a(90);
    cout << "a=" << a << endl;

    return 0;
}

string:在C++中使用string不会出现越界现象

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

struct MyString
{
    char *data;
};

int main(void)
{
#if 0
    MyString str2;
    str2.data = new char[1024];
    sizeof(MyString);sizeof(str2);
    strlen(str2.data);
#endif
    string str = "HelloWorld";
    cout << str << endl;
    cout << sizeof(string) << ' ' << sizeof(str) << endl;
    //length,size:实际元素的个数
    cout << str.length() << endl;
    cout << str.size() << endl;
    //capacity:实际存储空间的大小
    cout << str.capacity() << endl;

    //空间分配策略:
    //若新增数据与原数据大小和小于等于原来的两倍,则新空间大小为原来的两倍    
    //str = str + "123";
    //10+3 <= 10*2 --> 20

    //若新增数据与原数据大小和大于原来的两倍,则新空间大小为新旧数据大小之和
    str = str + "12345678901";
    //10+11 > 10 *2  --> 21 
    cout << str.length() << endl;
    cout << str.capacity() << endl;

    //clear:只是将数据清除并不会释放空间
    str.clear();
    cout << str.length() << endl;
    cout << str.capacity() << endl;

    if (str.empty())
    {
        cout << "str is empty\n";
    }

    return 0;
}

析构函数:释放对象用的,一个文件中有一个就够了

#include <iostream>
#include <string.h>
using namespace std;

class MyString
{
public:
    //析构函数:在对象释放的时候自动被调用
    ~MyString()
    {
        if (NULL != m_data)
        {
            cout << m_data << ':';
            delete []m_data;
        }
        cout << "~MyString()\n";
    }
    MyString(const char *data = NULL)
    {
        if (NULL == data)
        {
            m_data = NULL;
        }
        else
        {
            int len = strlen(data);
            m_data = new char[len+1];
            if (NULL == m_data)
            {
                cout << "new failed\n";
                return;
            }
            strcpy(m_data, data);
            cout << m_data << ':';
        }
        cout << "MyString(...)\n";
    }
    const char *data()
    {
        return m_data;
    }
private:
    char *m_data;
};
void fun()
{
    MyString str("HelloWorld");
    cout << str.data() << endl;
}
int main(void)
{
    //fun();
    //cout << "$$$$$$$$$$$$$$$$\n";
    MyString s1("1111");
    MyString s2("2222");
    MyString s3("3333");
    MyString s4("4444");

    return 0;
}

homework:

写一个类,可以对文件做打开-读写-关闭的操作

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • importUIKit classViewController:UITabBarController{ enumD...
    明哥_Young阅读 4,297评论 1赞 10
  • 一、温故而知新 1. 内存不够怎么办 内存简单分配策略的问题地址空间不隔离内存使用效率低程序运行的地址不确定 关于...
    SeanCST阅读 8,225评论 0赞 27
  • 1. 让自己习惯C++ 条款01:视C++为一个语言联邦 为了更好的理解C++,我们将C++分解为四个主要次语言:...
    Mr希灵阅读 3,053评论 0赞 13
  • C++文件 例:从文件income. in中读入收入直到文件结束,并将收入和税金输出到文件tax. out。 检查...
    SeanC52111阅读 3,212评论 0赞 3
  • 这两天一直在帮同事做VBA的编程。主要实现的是自动判断人员考核类型,以及自动匹配考核关系。是目前以来碰到的逻辑最复...
    Who_I_am阅读 214评论 0赞 0

友情链接更多精彩内容