为了账号安全,请及时绑定邮箱和手机立即绑定
  • (大家好,我是James。欢迎大家来到C++远征之封装篇)哲理景致令人神往,这里的知识饱含热情。下面我就作为向导带着大家一起来看一看封装篇中的主要内容。 那么首先登场的呢就是“类”和“对象”,它们呢是我们封装篇大戏中的领衔主演,不过咱们的“类”呢是抽象派,玩的呢就是概念,“对象”呢则真实而具体。那么有了主角之后呢,我们还是需要一些配角帮衬着剧情才够热闹,所以呢我们下面隆重为大家介绍各位配角。 首先呢是“数据成员和成员函数”,它们构成了精彩而完整的“类”。 其次呢我会为大家介绍的是“构造函数和析构函数”,它们呢描述了“对象”的生生死死。 “对象的复制与赋值”使“类”的定义充满艺术。 “对象的数组与对象指针”呢将应用形态发挥得淋漓尽致。 最后呢我会为大家介绍影子一样的“this指针”它贯穿于我们这部大戏的始终却很少崭露头角,它的加入使“类”与“对象”得以有机结合,更是为封装篇画龙点睛。 那么大家学完了这些之后相信大家的思维模式呢就逐步地从面向过程转向面向对象,从而可以处理更加复杂的程序,编程的水平呢也能与日俱增,届时大家就可以自娱自乐搞点儿小游戏……
    查看全部
    8 采集 收起 来源:课程简介

    2016-05-07

  • 从栈中实例化 Coordinate coor; coor.x=10; coor.y=20; 栈中实例化之后内存自动清除。 从堆中实例化 Coordinate *p=new Coordinate(); p->x=100; p->y=200; 堆中实例化不一定成功,所以需要判断一下:if(p==NULL){ return 0;} 堆中实例化之后需要清除内存,即:delete p;p=NULL;
    查看全部
  • getline()的原型是istream& getline ( istream &is , string &str , char delim ); 其中 istream &is 表示一个输入流,譬如cin;string&str表示把从输入流读入的字符串存放在这个字符串中(可以自己随便命名,str什么的都可以);char delim表示遇到这个字符停止读入,在不设置的情况下系统默认该字符为'\n',也就是回车换行符(遇到回车停止读入)。给大家举个例子: string line; cout<<"please cin a line:" getline(cin,line,'#'); cout<<endl<<"The line you give is:"line; 那么当我输入"You are the #best!" 的时候,输入流实际上只读入了"You are the ",#后面的并没有存放到line中(应该是在缓冲区里吧)。然后程序运行结果应该是这样的: please cin a line:You are the #best! The line you give is:You are the 而且这里把终止符设为#,你输入的时候就算输入几个回车换行也没关系,输入流照样会读入,譬如: please cin a line:You are the best! //这里输入了一个回车换行 Thank you! # //终止读入 The line you give is:You are the best! //换行照样读入并且输出 Thank you! 以上就是getline()函数一个小小的实例了。
    查看全部
    6 采集 收起 来源:C++ 初始String

    2018-03-22

  • 类内定义的成员函数,编译器会将其优先编译为内联函数。对于复杂的成员函数,则编译为普通的函数。 类外定义的成员函数的格式: 返回类型 类名::函数名(参数列表) 分文件类外定义: 定义一个类名.h的头文件,用来保存类的定义; 定义一个类名.cpp的文件,用来保存类中成员函数的定义,定义之前要加上#include "类名.h"
    查看全部
    6 采集 收起 来源:类外定义

    2018-03-22

  • 构造函数的规则和特点: 1、构造函数在对象实例化时被自动调用(有且仅有一次); 2、构造函数与类同名; 3、构造函数没有返回值(连 void 都不用写); 4、构造函数可以有多个重载形式(遵循重载规则,参数个数、类型、调用顺序不同等); 5、即使有多个构造函数,实例化对象时仅用到一个构造函数; 6、用户没有定义构造函数时,编译器自动生成一个构造函数。 内存分区 栈区:定义变量。内存由系统进行控制,释放和分配 堆区:关键字new,需要程序员自己管理释放 全局区:全局变量和静态变量 常量区:字符串和常量
    查看全部
  • 1.类的写法一般是public写在前面,private写在后面 2.建议定义私有成员时命名为 m_数据类型+成员名 如:string m_strName 以便区分数据成员,知道数据成员是定义在类里面的 形参是_+成员名,如:void getName(string _name) 3.只读:初始化,只有get函数,不设置set函数。
    查看全部
  • 从栈实例化对象:自动分配释放内存。 从堆实例化对象:手动分配释放内存。
    查看全部
  • 命名规则中的m是指member
    查看全部
  • #include <iostream> #include <string> using namespace std; class Teacher { public: void setName(string _name); string getName(); void setGender(string _gender); string getGender(); void setAge(int _age); int getAge(); void teach(); private: string m_strName; string m_strGender; int m_iAge; }; void Teacher::setName(string _name) { m_strName = _name; } string Teacher::getName() { return m_strName; } void Teacher::setGender(string _gender) { m_strGender=_gender; } string Teacher::getGender() { return m_strGender; } void Teacher::setAge(int _age) { m_iAge=_age; } int Teacher::getAge() { return m_iAge; } void Teacher::teach() { cout << "现在上课。。。。" << endl; } int main() { Teacher t; t.setName("李白"); t.setGender("男"); t.setAge(19); cout << t.getName() << ' ' << t.getGender() << ' ' << t.getAge() << endl; t.teach(); return 0; }
    查看全部
  • #include <iostream> #include <string> using namespace std; class Teacher { public: Teacher(string name = "Jim", int age = 1); Teacher(const Teacher &tea); ~Teacher(); void setName(string name); string getName(); void setAge(int age); int getAge(); private: string m_strName; int m_iAge; }; #include "Teacher.h" Teacher::Teacher(string name,int age):m_strName(name),m_iAge(age) { cout <<"Teacher(string name,int age)" <<endl; } Teacher::Teacher(const Teacher &tea) { cout <<"Teacher(const Teacher &tea)" <<endl; } Teacher::~Teacher() { cout <<"~Teacher()" <<endl; } void Teacher::setName(string name) { m_strName = name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int age) { m_iAge = age; } int Teacher::getAge() { return m_iAge; } #include <iostream> #include<stdlib.h> #include "Teacher.h" #include <string> using namespace std; void test(Teacher t) { } int main() { Teacher t1; Teacher t2(t1); system("pause"); return 0; }
    查看全部
  • #include <string> #include <iostream> using namespace std; class Teacher { public: Teacher(string name = "Jim",int age=1); Teacher(const Teacher &tea); void setName(string _name); string getName(); void setAge(int _age); int getAge(); private: string m_strName; int m_iAge; }; #include "Teacher.h" #include <iostream> using namespace std; Teacher::Teacher(string name,int age):m_strName(name),m_iAge(age) { cout <<"Teacher(string name,int age)" <<endl; } Teacher::Teacher(const Teacher &tea) { cout <<"Teacher(const Teacher &tea)" <<endl; } void Teacher::setName(string _name) { m_strName=_name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int _age) { m_iAge=_age; } int Teacher::getAge() { return m_iAge; } #include <string> #include <iostream> #include "Teacher.h" #include<stdlib.h> using namespace std; void test(Teacher t) { } int main() { Teacher t1; test(t1); system("pause"); return 0; }
    查看全部
  • #include <iostream> #include<string> using namespace std; class Teacher { public: Teacher(string name = "Jim",int age = 1,int m=100); void setName(string name); string getName(); void setAge(int age); int getAge(); int getMax(); private: string m_strName; int m_iAge; const int m_iMax; }; #include"Teacher.h" Teacher::Teacher(string name,int age,int m):m_strName(name),m_iAge(age),m_iMax(m) { cout <<"Teacher(string name,int age)" <<endl; } int Teacher::getMax() { return m_iMax; } void Teacher::setName(string name) { m_strName=name; } string Teacher::getName() { return m_strName; } void Teacher::setAge(int age) { m_iAge=age; } int Teacher::getAge() { return m_iAge; } #include <iostream> #include <string> #include <stdlib.h> #include "Teacher.h" using namespace std; int main(void) { Teacher t1("Merry",12,150); cout <<t1.getName() <<" " <<t1.getAge() <<" " <<t1.getMax() <<endl; system("pause"); return 0; }
    查看全部
  • 构造函数:出生--析构函数:死亡 对象的生命历程 申请内存—初始化列表-构造函数-参与运算-析构函数-释放内存 析构函数 ( 在对象销毁时,自动调用,归还系统资源 定义格式:~类名() ) 如果没有自定义,系统自动生成 析构函数在对象销毁时自动调用 析构函数没有返回值、没有参数也不能重载
    查看全部
    3 采集 收起 来源:C++ 析构函数

    2015-08-11

    1. 类的写法一般是public写在前面,private写在后面 2.建议定义私有成员时命名为 m_数据类型+成员名 如:string m_strName 以便区分数据成员,知道数据成员是定义在类里面的 形参是_+成员名,如:void getName(string _name) 3.只读:初始化,只有get函数,不设置set函数。


    查看全部
  • #include<iostream> #include<stdlib.h> #include<string> using namespace std; int main(void) { string name; cout << "Please input your name:"; getline(cin,name); //记住这条语句 if(name.empty()) { cout << "input is null.." << endl; system("pause"); //没有会闪退 return 0; //没有会出错 } if(name == "imooc") { cout << "you are a administrator" << endl; } cout << "hello " + name <<endl; cout << "your name length :" << name.size() << endl; //记住name.size()函数 以及连接用的“<<” cout << "your name first letter is : " << name[0] << endl; system("pause"); return 0; }
    查看全部
    3 采集 收起 来源:C++ 初始String

    2018-03-22

首页上一页1234567下一页尾页

举报

0/150
提交
取消
课程须知
本课程是C++初级课程 需要熟练掌握C++语言基础语法
老师告诉你能学到什么?
1、类的定义与对象实例化 2、string类型及属性封装 3、类外定义 4、构造函数与初始化列表 5、析构函数

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!