- 如何格式化输出bool类型的值?
在打印输出bool变量前使用boolalpha转换,使用noboolalpha解除转换。
#include//input output stream 输入输出头文件
using namespace std;
int main()
{
bool a;
a=100;
a=true;
cout<<a<<endl;//1
cout<<boolalpha<<a<<endl;//a true
}
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.声明类
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<<“价格:”<<pr1.get()<<endl;
product pr2;
pr2.set("pad",5000);
cout<<"价格:"<<pr2.get()<<endl;
//从群众中来 到群众中去
//product p3;
//product *pr3=&p3;
product *pr3=new product;//
//错误处理
pr3->set("电话手表",300);
cout<<"价格:"<<pr3->get()<<endl;
delete pr3;
pr3 = NULL;
}
作业2:定义一个类 Array,定义二维数组成员,int arr[3][4];
重载函数print(),分别实现常规打印数组元素和以一定格式打印数组
数组初始化:init函数向二维数组中存入数据 如下:行下标i 列下标是j arr[i][j]=i+j
0 1 2 3
1 2 3 4
2 3 4 5
print() 函数打印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;
class Array
{
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<<arr[i][j]<<" ";
}
cout<<endl;
}
}
//非常规打印
void print(char sp)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<4;j++)
{
if(j<3)
{
cout<<arr[i][j]<<sp;
}
else
{
cout<<arr[i][j]<<endl;
}
}
}
}
};
int main()
{
Array arr;
arr.init();
arr.print();
arr.print(’?’);
}
一、封装
1.理解
封装 是面向对象程序设计的最基本特征(封装 继承 多态),把属性与行为合成一个整体
1、封装,是面向对象程序设计最基本的特性。把数据(属性)和函数(操作)合成一个整体,这在计算机世界中是用类与对象实现的。
封装,把客观事物封装成抽象的类,并且类可以把自己的数据和方法只让可信的类或者对象操作,对不可信的进行信息隐藏。
备注:有2层含义(把属性和方法进行封装 对属性和方法进行访问控制)
第一层含义:把属性和方法进行封装
成员变量,C++中用于表示类属性的变量
成员函数,C++中用于表示类行为的函数
实例:封装的好处
#include<iostream>//input output stream 输入输出头文件
#include<string>
using namespace std;
class Teacher
{
public:
string name;
int age;
// int id;
void init(string n,int a)
{
name = n;
age = a;
}
int get_age()
{
return age;
}
int show()
{
cout<<"老师:"<<name<<" 年龄:"<<age<<endl;
//cout<<id;//修改类
}
};
void print(Teacher &x)//Teacher &x=t
{
x.show();//如果show函数内部修改 这里不用改
cout<<x.get_age()<<endl;
}
int main()
{
Teacher t;
t.init("小明",18);
print(t);
}
2.类成员的访问控制
第二层含义:对属性和方法进行访问控制
在程序设计中不是每个属性都需要对外公开,而有一些类的属性是对外公开的;
因此在设计类时必须在类的表示法中定义属性和行为的公开级别,将不对外公开的属性使用private进行修饰。
因为被private修饰,所以外部无法对其进行写入与读取的操作,
3.类的访问控制关键字
class 类名
{
public://修饰的成员变量和成员函数可以在类的内部及类外部访问
成员变量及成员函数;
private://修饰的成员变量和成员函数只有类内可以访问
成员变量及成员函数;
protected://修饰的成员变量和成员函数类内及其子类可以访问
成员变量及成员函数;
};
例子:
#include<iostream>//input output stream 输入输出头文件
using namespace std;
class Person
{
private:
string passwd;//密码 私有
protected:
int money;// 保护
public:
int apple;//公有
void show()
{
cout<<"密码:"<<passwd<<"钱:"<<money<<"苹果:"<<apple<<endl;
}
};
int main()
{
Person ps1;
//ps1.passwd = 666;//错误 私有属性 类外不能访问
ps1.apple = 66;//正确 公有属性 类外可以访问
ps1.show();//没错 都可以访问
}
练习:求长方体体积
#include//input output stream 输入输出头文件
using namespace std;
class Cube
{
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<<c.get_v()<<endl;
}
续:编写一个普通函数(全局函数) 比较两个长方体是否相同(长宽高==长宽高)
#include//input output stream 输入输出头文件
using namespace std;
class Cube
{
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;
}
};
void judge(Cube &x,Cube &y)
{
if((x.get_longth()==y.get_longth())&&(x.get_width()==y.get_width()&&(x.get_height()==y.get_height())))
{
//相同
cout<<“相同”<<endl;
}
else
{
//不同
cout<<“不同”<<endl;
}
}
int main()
{
Cube c;
c.set_value(2,3,5);
cout<<c.get_v()<<endl;
Cube c1;
c1.set_value(2,32,5);
judge(c,c1);
}
二、类的声明与实现分开
实例:
#include//input output stream 输入输出头文件
using namespace std;
class Cube
{
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();
};
double Cube::get_longth()
{
return longth;
}
double Cube::get_width()
{
return width;
}
double Cube::get_height()
{
return height;
}
double Cube::get_v()
{
return longthwidthheight;
}
void Cube::set_value(double l,double w,double h)//std::endl
{
longth = l;
width = w;
height = h;
}
void judge(Cube &x,Cube &y)
{
if((x.get_longth()==y.get_longth())&&(x.get_width()==y.get_width()&&(x.get_height()==y.get_height())))
{
//相同
cout<<“相同”<<endl;
}
else
{
//不同
cout<<“不同”<<endl;
}
}
int main()
{
Cube c;
c.set_value(2,3,5);
cout<<c.get_v()<<endl;
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)<<endl;
}
int main()
{
Test a;
a.init(45,33);
a.print();//12
}
三、struct与class区别
c++中类class从c的struct 扩展而来
1.类与结构体的使用时机
1)在表示诸如点、矩形等主要用来存储数据的轻量级对象时,首选struct
2) 在表示数据量大、逻辑复杂的大对象时,首选class。
3)在表现抽象和多级别的对象层次时,class是最佳选择。
因此struct常用来处理作为基类型对待的小对象,而class来处理某个商业逻辑。
2.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<iostream>//input output stream 输入输出头文件
using namespace std;
struct Stu
{
int age;
int id;
void fun()
{
}
};
class Teacher
{
int a;
int b;
};
int main()
{
Stu s;
s.age=90;//可以 默认访问权限公有
s.id=80;//可以 默认访问权限公有
Teacher t;
t.a=88;//错 默认访问权限私有
t.b=33;//错 默认访问权限私有
}
练习:定义类Array 向数组中输入整数 并求出数组元素中的最大值,最小值和平均值
1.数组成员变量private修饰
int *p; int length
length为数组的长度;
2.成员函数input(int len);
功能:cin方式,输入数组的值
数组动态创建new(按照输入的长度,创建数组)
3.成员函数 int Max(); 要求带返回值
4.成员函数 int Min();
5.成员函数 int Avg();
main 输出函数的返回值
实现:
#include<iostream>//input output stream 输入输出头文件
#include<cstdlib>
using namespace std;
class Array
{
private:
int *p;
int length;
public:
void input(int len);
int Max();
int Min();
int Avg();
};
void Array::input(int len)
{
length = len;
p = new int[length];
if(NULL==p)
{
cout<<"malloc error"<<endl;
exit(-1);
}
cout<<"请输入"<<length<<"个数据:"<<endl;
for(int i=0;i<length;i++)
{
cin>>p[i];
}
}
int Array::Max()
{
static int max = p[0];
for(int i=1;i<length;i++)
{
if(max<p[i])
{
max = p[i];
}
}
return max;
}
int Array::Min()
{
static int min = p[0];
for(int i=1;i<length;i++)
{
if(min>p[i])
{
min = p[i];
}
}
return min;
}
int Array::Avg()
{
int sum = p[0];
for(int i=1;i<length;i++)
{
sum += p[i];
}
static int avg = sum/length;
return avg;
}
int main()
{
Array a;
a.input(3);
cout<<"最大值:"<<a.Max()<<endl;
cout<<"最小值:"<<a.Min()<<endl;
cout<<"平均值:"<<a.Avg()<<endl;
}
四、构造函数–构造器
1.设计构造函数原因
#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)<<endl;
}
int main()
{
Test a;
// a.init(45,33);
a.print();//12
}
2.构造函数
1)概念
构造函数是特殊成员函数 功能:构造对象时 初始化对象 每个类都有构造函数
2)如何定义
- 构造函数名必须与类同名
- 构造函数的访问权限一般是public
- 构造函数没有返回值 可以有形参—可以重载 Test(int,int)
3)构造函数的调用
一般是创建对象 自动调用
自动调用:一般情况下C++编译器会自动调用构造函数
手动调用:在一些情况下则需要手工调用构造函数
4)无参构造:
#include<iostream>//input output stream 输入输出头文件
using namespace std;
class Test
{
private:
int x;
int y;
public:
Test();
void print();
};
//构造函数 成员变量初始化
Test::Test()
{
cout<<"我--构造!"<<endl;
x = 45;
y = 33;
}
void Test::print()
{
cout<<(x-y)<<endl;
}
int main()
{
cout<<"实例化之前...."<<endl;
Test a;//实例化对象 自动调用构造函数
cout<<"实例化之后。。。"<<endl;
a.print();//12
}
作业:实现一个学生类
成员变量:姓名 年龄 学号
成员函数:
输入函数:输入学生信息
找最大值函数:找到年龄最大的人并输出相关信息