-
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:
写一个类,可以对文件做打开-读写-关闭的操作
网友评论