- 如何格式化输出bool类型的值?
在打印输出bool变量前使用boolalpha转换,使用noboolalpha解除转换。
#include//input output stream 输入输出头文件
using namespace std;
int main()
{
bool a;
a=100;
a=true;
cout< cout<
2.引用的特点是什么?
3条
1)必须初始化
2)不能改变指向
3)普通引用 不能用常量和临时值初始化
3.引用与指针的区别
1)引用必须初始化 指针可以初始化 2)引用不能改变指向 指针可以改变指向 3)指针操作 使用前一般需要检查(if(NULL==p)) 引用不需要 4)存在空指针 不存在空引用
4.int a = 10; 定义变量a的引用b为:int &b=a;
5.定义一个c++风格的字符串s1
string s1;
6.
string s1 = “hello world”;
char buf[100];
利用sprintf函数,将字符串s1转换成c风格buf中
sprintf(buf,"%s",s1.c_str());
7.声明一个常引用
const int &b=100;
8.常引用和普通引用能不能指向常量?
const int &b=100;//对
int &b=100;//错
9.使用命名空间的作用
避免同名冲突(避免命名空间污染)
10.在堆空间为变量a分配一块空间
int *p=new int;
delete p;
11.动态为数组申请一块连续空间
int *p=new int[4]
delete []p;
12.简述new delete和malloc free的区别----重要
c malloc free 函数 申请地址没有指定类型 不进行初始化
c++ new delete 关键字 申请地址有指定类型 可以初始化
13.函数重载的条件是什么?—重要
条件:
1)参数列表不同(参数类型或者参数个数)
2)函数名必须相同
14.定义fun函数,参数列表三个参数,给其中一个参数加上默认参数
规则:必须从右向左
void fun(int a,int b,int c=90);
作业1:创建一个产品类product 成员变量自己定义(string name,int price) 使用set函数及get函数进行赋值 及获取结果(price)
在main中创建3个产品对象并赋值 打印
#include//input output stream 输入输出头文件
using namespace std;
//1.声明类 //从群众中来 到群众中去 } 作业2:定义一个类 Array,定义二维数组成员,int arr[3][4]; 数组初始化:init函数向二维数组中存入数据 如下:行下标i 列下标是j arr[i][j]=i+j 实现: class Array }; 一、封装 1.理解 2.类成员的访问控制 3.类的访问控制关键字 练习:求长方体体积 class Cube 续:编写一个普通函数(全局函数) 比较两个长方体是否相同(长宽高==长宽高) #include//input output stream 输入输出头文件 class Cube }; void judge(Cube &x,Cube &y) } int main() } 二、类的声明与实现分开 class Cube double Cube::get_longth() void Cube::set_value(double l,double w,double h)//std::endl void judge(Cube &x,Cube &y) } int main() } 笔试题 实现: 三、struct与class区别 1.类与结构体的使用时机 2.struct与class区别 四、构造函数–构造器 1.设计构造函数原因 2.构造函数 作业:实现一个学生类
class product
{
public:
string name;
int price;
void set(string n,int pri)
{
name = n;
price = pri;
}
int get()
{
return price;
}
};
int main()
{
//2.创建对象
product pr1;//栈
pr1.set(“手机”,8000);
cout<<“价格:”<product pr2;
pr2.set("pad",5000);
cout<<"价格:"<//product p3;
//product *pr3=&p3;
product *pr3=new product;//
//错误处理
pr3->set("电话手表",300);
cout<<"价格:"<
重载函数print(),分别实现常规打印数组元素和以一定格式打印数组
0 1 2 3
1 2 3 4
2 3 4 5print() 函数打印3行4列的二维数组--常规格式
print(char sp);//sp可以是某个字符 打印输出如下
0#1#2#3
1#2#3#4
2#3#4#5
#include//input output stream 输入输出头文件
using namespace std;
{
public:
int arr[3][4]; //数组初始化
void init()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
arr[i][j] = i+j;
}
}
}
//常规打印
void print()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
cout<
int main()
{
Array arr;
arr.init();
arr.print();
arr.print(’?’);
}
封装 是面向对象程序设计的最基本特征(封装 继承 多态),把属性与行为合成一个整体
1、封装,是面向对象程序设计最基本的特性。把数据(属性)和函数(操作)合成一个整体,这在计算机世界中是用类与对象实现的。
封装,把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。
备注:有2层含义(把属性和方法进行封装 对属性和方法进行访问控制)
第一层含义:把属性和方法进行封装
成员变量,C++中用于表示类属性的变量
成员函数,C++中用于表示类行为的函数 实例:封装的好处
#include
第二层含义:对属性和方法进行访问控制 在程序设计中不是每个属性都需要对外公开,而有一些类的属性是对外公开的;
因此在设计类时必须在类的表示法中定义属性和行为的公开级别,将不对外公开的属性使用private进行修饰。
因为被private修饰,所以外部无法对其进行写入与读取的操作,
class 类名
{
public://修饰的成员变量和成员函数可以在类的内部及类外部访问
成员变量及成员函数;
private://修饰的成员变量和成员函数只有类内可以访问
成员变量及成员函数;
protected://修饰的成员变量和成员函数类内及其子类可以访问
成员变量及成员函数;
};例子:
#include
#include//input output stream 输入输出头文件
using namespace std;
{
private:
double longth;
double width;
double height;
public:
void set_value(double l,double w,double h)
{
longth = l;
width = w;
height = h;
}
double get_v()
{
return longthwidthheight;
}
};
int main()
{
Cube c;
c.set_value(2,3,5);
cout<
using namespace std;
{
private:
double longth;
double width;
double height;
public:
void set_value(double l,double w,double h)
{
longth = l;
width = w;
height = h;
}
double get_v()
{
return longthwidthheight;
}//获取私有属性的值
double get_longth()
{
return longth;
}
double get_width()
{
return width;
}
double get_height()
{
return height;
}
{
if((x.get_longth()==y.get_longth())&&(x.get_width()==y.get_width()&&(x.get_height()==y.get_height())))
{
//相同
cout<<“相同”<
else
{
//不同
cout<<“不同”<
{
Cube c;
c.set_value(2,3,5);
cout<Cube c1;
c1.set_value(2,32,5);
judge(c,c1);
实例:
#include//input output stream 输入输出头文件
using namespace std;
{
private:
double longth;
double width;
double height;
public:
void set_value(double l,double w,double h);//声明
double get_v();//声明
double get_longth();
double get_width();
double get_height();
};
{
return longth;
}
double Cube::get_width()
{
return width;
}
double Cube::get_height()
{
return height;
}
double Cube::get_v()
{
return longthwidthheight;
}
{
longth = l;
width = w;
height = h;
}
{
if((x.get_longth()==y.get_longth())&&(x.get_width()==y.get_width()&&(x.get_height()==y.get_height())))
{
//相同
cout<<“相同”<
else
{
//不同
cout<<“不同”<
{
Cube c;
c.set_value(2,3,5);
cout<Cube c1;
c1.set_value(2,32,5);
judge(c,c1);
下面是类测试程序 设计出使用如下测试程序的类:
int main()
{
Test a;
a.init(45,33);
a.print();//12
}
#include//input output stream 输入输出头文件
using namespace std;
class Test
{
private:
int x;
int y;
public:
void init(int,int);
void print();
};void Test::init(int m,int n)
{
x = m;
y = n;
}
void Test::print()
{
cout<<(x-y)<
c++中类class从c的struct 扩展而来
1)在表示诸如点、矩形等主要用来存储数据的轻量级对象时,首选struct
2) 在表示数据量大、逻辑复杂的大对象时,首选class。
3)在表现抽象和多级别的对象层次时,class是最佳选择。
因此struct常用来处理作为基类型对待的小对象,而class来处理某个商业逻辑。
c++ 类从c中struct发展而来 c++中建议将struct当做c中struct使用
1)c中struct与c++中class区别
区别一:struct 不可以封装函数
区别二:struct 默认访问权限是公有 class默认访问权限私有2)c++中struct与c++中class区别
class默认访问权限私有 struct 默认访问权限公有
实例:
#include
#include//input output stream 输入输出头文件
using namespace std;
class Test
{
private:
int x;
int y;
public:
void init(int,int);
void print();
};void Test::init(int m,int n)
{
x = m;
y = n;
}
void Test::print()
{
cout<<(x-y)<
1)概念
构造函数是特殊成员函数 功能:构造对象时 初始化对象 每个类都有构造函数
2)如何定义
- 构造函数名必须与类同名
- 构造函数的访问权限一般是public
- 构造函数没有返回值 可以有形参—可以重载 Test(int,int)3)构造函数的调用
一般是创建对象 自动调用
自动调用:一般情况下C++编译器会自动调用构造函数
手动调用:在一些情况下则需要手工调用构造函数
4)无参构造:
#include
成员变量:姓名 年龄 学号
成员函数:
输入函数:输入学生信息
找最大值函数:找到年龄最大的人并输出相关信息



