-
Student stu1() 这样写是不是有问题? 应该没有括号吧.查看全部
-
#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; cout << "your name first letter is:" << name[0] << endl; system("pause"); return 0; }
查看全部 -
getline(cin, 对象, 自定义的停止输入标志(默认为回车)),如:getline(cin,line,'#');
功能:用于字符读入,将输入流的字符赋给第二个参数
查看全部 -
内存分区 栈区 :intX = 0; int*p = NULL; 堆区 : int*p = new int[10]; 全局区 :存储全局变量及常态变量 常亮区 : string str = "hello"; 代码区 :存储逻辑代码的二进制
查看全部 -
定义一个Teacher类,要求分别采用同文件类外定义和分文件类外定义的方式文成
数据成员:
名字
年龄
性别
成员函数:
数据成员的数据封装函数
授课teach
Teacher.h(头文件)
#include<string>
#include<iostream>
using namespace std;
class Teacher
{
public:
Teacher();
Teacher(string name, int age=10);
void setName(string name);
string getName();
void setAge(int age);
int getAge();
private:
string m_strName;
int m_iAge;
};
Teacher.cpp(源文件)
#include"Teacher.h"
Teacher::Teacher()
{
m_strName = "ZYJ";
m_iAge = 5;
cout << "Teacher()" << endl;
}
Teacher::Teacher(string name,int age)
{
m_strName = name;
m_iAge = age;
cout << "Teacher(string name, int age)" << 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;
}
text.cpp(源文件)
#include<iostream>
#include<stdlib.h>
#include<string>
#include "Teacher.h"
using namespace std;
int main(void)
{
Teacher t1;
Teacher t2("ZYJ1", 22);
Teacher t3("ZTJ3");
cout << t1.getName() << " " << t1.getAge() << endl;
cout << t2.getName() << " " << t2.getAge() << endl;
cout << t3.getName() << " " << t3.getAge() << endl;
system("pause");
return 0;
}
查看全部 -
#include <iostream>
#include <string>
#include<stdlib.h>
using namespace std;
/*************************************************/
/*数据的封装
定义一个Student类,含有如下信息
1.姓名:name
2.性别:gender
3.学分(只读);score
4.学习:study*/
class Student
{
public:
//由于只有学分只读不写的所以其他的元素都要写上get和set函数
void setName(string _name)
{
m_strName = _name;
}
string getName()
{
return m_strName;
}
void setGender(string _gender)
{
m_strGender = _gender;
}
string getGender()
{
return m_strGender;
}
int getScore()
{
return m_iScore;
}
void initScore()//定义一个学分的初值
{
m_iScore = 0;
}
void study(int _score)//传入一个int类型的形参(分数)
{
m_iScore += _score;//与总分相加
}
private:
string m_strName;//添加一个命名的规则在名字前面加上m_并且在加上它的数据类型,这样写的好处是让我们知道这是一个定义在类里面的成员
string m_strGender;
int m_iScore;
};
int main(void)
{
Student stu;//实例化一个类的对象
stu.initScore();//初始化一个学分
stu.setName("zyj");
stu.setGender("女");
stu.study(10);
stu.study(20);
cout << stu.getName() << " " << stu.getGender() << " " << stu.getScore() << endl;
system("pause");
return 0;
}
查看全部 -
#include <iostream>
#include <stdlib.h>
#include <string>
#include "Teacher.h"
using namespace std;
/* Teacher 类
定义无参构造函数
定义有参构造函数
数据:名
年龄
成员函数
数据成员的封装函数
*/
int main(void)
{
Teacher t1;
Teacher t2("merry",15);
Teacher t3("James");
cout << t1.getName() << " " << t1.getAge() << endl;
cout << t2.getName() << " " << t2.getAge() << endl;
cout << t3.getName() << " " << t3.getAge() << endl;
system("pause");
return 0;
}
#include "Teacher.h"
#include <iostream>
Teacher::Teacher()
{
m_strName = "Jim";
m_iAge = 5;
cout << "Teacher()" << endl;
}
Teacher::Teacher(string _name , int _age)
{
m_strName = _name;
m_iAge = _age;
cout << " Teacher(string _name , int _age)" << 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 <string>
using namespace std;
/*
定义一个Student类,含有如下信息;
1.姓名:name
2.性别:gender
3.学分(只读):score
4.学习:study
*/
class Teacher
{
public:
Teacher();
Teacher(string _name, int _age = 20);
void setName(string _name);
string getName();
void setAge(int _age);
int getAge();
private:
string m_strName;
int m_iAge;
};
查看全部 -
demo.cpp的头文件为:#include <iostream> #include <stdlib.h> #include <string> #include "teacher.cpp" teacher.h的头为 #include <string> using namespace std; tercher.cpp的头为: #include "teacher.h"查看全部
-
栈:程序每调用一个函数后,建立一个新的栈帧空间保存当前调用函数中的所有局部变量,也就是说每个函数里面的变量都是在这个函数的栈帧内, 堆:程序运行过程中,向操作系统(OS)申请的系统内存(Mem),系统返回堆空间的地址。 栈帧:变量的所在地 ,变量可直接存取数值 堆: 向系统申请的内存空间,通过函数内的指针变量,赋值指针申请内存返回的地址,用于存取堆空间里的数据 不同之处: 栈帧:在函数结束时会自动释放, 堆: 需要主动申请释放,或者等程序运行结束后。 操作系统会自动释放程序运行期间申请的内存空间。 所谓变量,在经过编译器,编译、链接之后,是直接的内存地址。计算机直接对内存地址存取数据。查看全部
-
封装 #include<iostream> #include<stdlib.h> #include<string> using namespace std; class Student { public: void setName(string name){m_strName=name;} string getName() { return m_strName; } void setGender(string _gender) {m_strGender=_gender;} string getGender() { return m_strGender; } int getScore() { return m_iScore; } void initScore() { m_iScore=0; } void study(int _score) { m_iScore += _score; } private: string m_strName; string m_strGender; int m_iScore; }; int main() { Student stu; stu.initScore(); stu.setName("zhangdan"); stu.setGender("女"); stu.study(5); stu.study(3); cout<<stu.getName()<<" "<<stu.getGender()<<" "<<stu.getScore()<<endl; return 0; }查看全部
-
成员函数分文件类外定义可以这样: 类的定义和成员函数的声明放在头文件里面; 成员函数的定义放在一个cpp文件(要有#include 头文件名)里面; 主函数main()放在另一个cpp文件(要有#include 头文件名)里面。查看全部
-
类(抽象概念),对象,附件:数据成员和成员函数(构成了精彩而完整的类);构造函数和析构函数(描述了对象的生生死死);对象复制和对象赋值(使类的定义充满艺术);对象数组和对象指针(将应用型态发挥到淋漓尽致);this指针(影子)。面向过程到面向对象。查看全部
-
封装篇:数据成员 成员函数 构造函数 析构函数 对象赋值 对象赋值 对象数组 对象指针 this 指针!!!!!查看全部
-
封装成员函数步骤: 1、先写关键字pullic,private; 2、然后在private中定义全部成员。//加上类型符号string或者int之类的识别符,记得加头文件#include. 3、再写public中罗列所有声明的成员函数: void setName(string name); string getName(); ..... 4、开始内外定义: void "类"::setName(string name) { //把priavte的成员逐个写进去//"成员" = "_同等成员名称" } string "上述成员"::getName() { return "上述成员";//用来实现 } 5、在主函数中使用,实例化对象。 声明一下成员对象的首地址,如: Teacher t;// t关键字入口 通过"."访问 关键字.对象(初始化);如:t.setName("孔子"); ....//所有数据 6、输出所有成员//cout<<"成员"<<....<<endl; 7、开始调试查看全部
-
A. 当构造函数没有参数时也叫做默认构造函数。 B. 一个类可以没有默认构造函数,有别的构造函数也可以实例化对象. C. 当构造函数有参数,但每个参数都有默认值时,也称为默认构造函数。 D. 当实例化对象时,如果不使用任何参数,则调用的是默认构造函数。 默认构造函数: (1)默认构造函数是有默认值的构造函数 (2)系统自动生成的,无参数,无函数体代码的构造函数查看全部
举报