目录
一、面向对象编程(OOP--Object Oriented Programming)
1、定义
2、相关名词解释
3、 C语言与C++
4、二者的差异
5、C++对C的改进
注:是一些相关笔记和代码
对象的含义是指具体的某一个事物,即在现实生活中能够看得见摸得着的事物。在面向对象程序设计中,对象所指的是计算机系统中的某一个成分。在面向对象程序设计中,对象包含两个含义,其中一个是数据,另外一个是动作。对象则是数据和动作的结合体。(解释来源于百度百科)
OOP是程序设计过程化的一种方法,软件架构的一种思想。
OOP基本原则是程序由单个能够起到子程序作用的单元数据或对象组合而成,以达到软件工程的三个主要目标:重用性、灵活性和扩展性。
(1)重用性:代码重复使用,以减少代码量;
(2)灵活性:软件系统可以容易的新增需求,基本构建也可以轻松的扩展;
(3)扩展性:软件系统可以容易的新增需求,基本构件也可轻松扩展功能。
(1)对象--Object
可以对其做一些事情的东西,一个对象有状态、行为和标识三部分,对象是具体的实现。
状态:用来描述对象的基本特征
行为:用来描述对象的功能
标识:指的是对象在内存中都有一个唯一的标识(地址),用来与其他对象区分开来。
(2)类--class
共享相同数据和方法的对象集合。描述了一类事物的抽象特点,类的方法和属性被称为“成员”,类是一类事物的抽象。
(3)封装--enscapsulation
将数据和方法(操作)捆绑在一起,这种关系中自雷可以对基类进行拓展、覆盖、重定义。
(4)组合
既是类之间的关系也是对象之间的关系。在这种关系之中一个对象或者包含了其他的对象和类。组合描述了“有”关系。
(5)多态--polymorphism
类型理论中的概念,一些名称可以表示很多不同类的对象。
(6)动态绑定(静态联编)
也称静态类型,指的是一个对象或者二表达式的类型在编译时确定。
(7)消息传递
指的是一个对象调用了你给一个对象的方法(或者成员函数)。
(8)方法
也称成员函数,是指对象上的操作,作为声明的一部分来定义。方法定义了可以对一个对象执行哪些操作。
(1)二者的区别
C++是对C语言的升级和完善
(2)C语言有什么缺陷(与C++进行比较)
1、不严谨
1.
int main(int argc, const char *argv[])
{
char *p = "hello";
p = 'H';
return 0;
}
cpp---c++文件
编译 g++ file.cpp
c语言只是警告,c++会报错
2.
int main(int argc, const char *argv[])
{
const int i;
return 0;
}
c++会报错,i没有赋值 改:const int i = 100;
2、名字冲突 作用域
c++可以进行函数名重载
3、面向过程
有一定的顺序,过程
4、无法解决大规模问题
1)第一个C++程序
#include //输入和输出的头文件
//#include //printf的头文件--C语言
#include
using namespace std;
int main()
{
printf("hello world");
//std::cout << "hello world" << std::endl;//endl相当于换行符'\n'
cout << "hello world" << '\n';
//std::cout << "hello world" ;//cout输出,打印语句 '<'向右边,输入'>'向左边
cout << "hello world" << endl;
return 0;
}
2)输入和输出
主要用于代码的调试
cout:输出流对象
cin:输入流对象
1.cout函数
#include
using namespace std;
int main()
{
int a = 100;
float f = 3.14;
char ch = 'a';
double b = 1.24;
string str = "hello";//字符串的类
char buf[10] = "world";
//printf("%d %f %c %lf %s\n", a, f, ch, b, str);
//cout 会自动匹配不同变量的数据类型
//cout << a << f << ch << b << str << endl;打印一行
cout << a <
using namespace std;
int main()
{
int a;
char ch;
float b;
double d;
string str;
char buf[100];
cout << "Please input:" << endl;//提示,要用cout函数打印
cout << "Please input:\n";//也有换行的效果
//输入
cin >> a;
cin >> ch;
cin >> b;
cin >> d;
cin >> str;
cin >> buf;
//cin >> a >> ch >> b >> d >> str >> buf >>endl;//也是可以的
//输出
cout << a << " " << ch << " " << b << endl;
cout << d << " " << str << " " << buf << endl;
return 0;
}
3)缩进
#include
#include //setw的头文件 setw缩进的函数
using namespace std;
int main()
{
cout << setw(20) << "hello" << endl;
//输出内容按照指定格式对齐
return 0;
}
4)引用类型
(1)C语言中的取别名-typedef
#include
typedef int data_t;
typedef int (*ptrArray)[int];
int main()
{
int (*p)[int];//前后都有数据类型
ptrArray p;//两个p是等价的
return 0;
}
(2)c++新增,取别名,给变量取别名--reference
#include
using namespace std;
int main()
{
int a = 100;
//1.&a 给变量取地址
int &b = a;//a取别名为b
cout << a << endl;//打印a的值
cout << &a << endl;//打印a的地址
cout << b << endl;
cout << &b << endl;
return 0;
}
注意:两个变量不能取同一个别名
int c;
int &b = c;//error
5)引用传参
可以改变实参的值
#include
using namespace std;
void change(int *x, int *y)
{
*x ^= *y;
*y ^= *x;
*x ^= *y;
cout << "change: " << *x << " " << *y << endl;
}
int main()
{
int a = 1, b = 2;
change(&a, &b);
cout << "main:" << a << " " << b << endl;
return 0;
}
特点:更高效,因为没有开辟新的空间,因为地址变量是一样的
int *p
cout << *p << endl;
因为指针存在野指针,不安全,闯红灯行为,可能出事可能不出事
6)内联函数
#include
using namespace std;
void fun(void)
{
cout << "call fun" << endl;
}
int main()
{
int n = 0;
while(n < 5)
{
fun();
n++;
}
return 0;
}
c++提供高效方法:内联函数(inline)
语法:inline 存储类型 数据类型 函数名(参数列表);
#include
using namespace std;
inline void fun(void)
{
cout << "call fun" << endl;
}
int main()
{
int n = 0;
while(n < 5)
{
fun();
n++;
}
return 0;
}
不会跳转--把循环替换为
cout << "call fun" << endl;
cout << "call fun" << endl;
cout << "call fun" << endl;
cout << "call fun" << endl;
cout << "call fun" << endl;
打印5个call fun---空间换时间
内联函数的限制:
1.可以洁身时间,但是增加了长度
2.一般规模较小的才使用,称为内置函数
3.多个源文件,分开编译,所以内联函数定义在头文件中
4.内置函数不能包含复杂的控制语句,如Switch语句
7)函数重载
大规模代码,函数重名不可避免
函数重载:在同一个作用域下,函数名相同,参数不同(个数不同,类型不同,个数以及类型都不同),返回值可相同可不同
eg:
#include
using namespace std;
int add(int x)
{
return x+x;
}
int add(int x, int y)
{
return x+y;
}
double add(double a, double b, double c)
{
return a+b+c;
}
double add(double a, double b)
{
return a+b;
}
float add(float a, float b)
{
return a+b;
}
char add(char a, char b)
{
return a+b;
}
int main()
{
//函数重载传递不同的参数,就执行不同的函数
cout << add(10) << endl;//20
cout << add(5,6) << endl;//11
//1.在小数后面加上f----float类型
cout << add(1.2f,2.4f) << endl;//小数是double类型
//2.解除double add(double double);---解除屏蔽
//3.直接定义变量的值
float a=1.2, b=2.4;
cout << add(a,b) << endl;
cout << add(1.1, 1.2, 1.3) << endl;//doublea,b,c相加
cout << add('1','1') << endl;//字符相加
return 0;
}
当输入的是小数时,都是double类型,如何解决?
//1.在小数后面加上f----float类型
cout << add(1.2f,2.4f) << endl;//小数是double类型
//2.解除double add(double double);---解除屏蔽
//3.直接定义变量的值
float a=1.2, b=2.4;
cout << add(a,b) << endl;
8)默认参数
#include
using namespace std;
int add(int x=100);
int add(int x, int y);
float add(float a, float b);
double add(double a, double b);
char add(char a, char b);
double add(double a , double b , double c);
int main()
{
//1.可以不用传参,但是一旦传参必须按照传递的参数
cout << add() << endl;
cout << add(1.2, 1.3) << endl;
return 0;
}
int add(int x)
{
return x+x;
}
int add(int x, int y)
{
return x+y;
}
float add(float a, float b)
{
return a+b;
}
#if 1
double add(double a, double b)
{
return a+b;
}
#endif
char add(char a, char b)
{
return a+b;
}
double add(double a , double b , double c)
{
return a+b+c;
}
默认参数,函数调用时可以不用传参
如果给某一参数设置了默认值,那么在参数表中其后的所有参数都必须也设置默认值
如果进行了函数声明,在声明中设置默认值就可
5.1结构体
5.1.1结构体关键字struct可以省略
#include
using namespace std;
struct Student{
char name[32];
int age;
float socre;
void fun(void)
{
cout << "fun" << endl;
}
};
int main()
{
struct Student stu;
//可写为Student stu;关键字struct可以省略
cin >> stu.name;
cin >> stu.age;
cin >>stu.socre;
cout << stu.name << " " << stu.age << " " << stu.socre <
5.1.2通过函数访问变量
#include
using namespace std;
struct Student{
//成员变量
char name[32];
int age;
float socre;
//成员函数
void input_info(void)
{
cin >> name;
cin >> age;
cin >> socre;
}
void output_info(void)
{
cout << name << age << socre <
5.2类和对象
面向对象的程序设计的思想和人们日常生活中处理问题的思路是相似的。在自然世界和社会生活中,一个复杂的失误总是由许多部分组成。
任何对象都应该有两个要素,即行为和属性,能根据外界信息进行操作。
类就是把具有相同属性方法的对象归在一起,抽象的概括起来
类是共同概念的集合,对象是描述的具体的实物
struct 和 class的区别:两个的权限不一样
成员变量是私有的,不能去访问
可改为
class Student{//将前面的struct换成class
//成员变量
char name[32];
int age;
float socre;
}
public:
函数;
#include
#include
using namespace std;
//struct : public默认的权限,公有的
//class : private默认的权限,私有的
class Student{
public:
//成员函数----行为
void input_info(const char *p, int n, float f)
{
strcpy(name, p);
age = n;
socre = f;
}
void output_info(void)
{
cout << name << " " << age << " " << socre <
直接修改和通过函数修改有什么区别?
通过函数来设置参数检测,如果把private改成public,直接修改,那么设计者是的规则,用户没有体会到,通过函数来修改更安全
为什么要把一些变量设置为私有的?
不设置为私有,别人都可以访问
封装
把细节藏在类中,不希望外面能随便操作它
使得代码模块化(将属性和行为作为一个整体,变现生活中的实物,并加以权限控制)
类和对象的关系
类:是一种自定义的数据类型,是抽象的,不占内存空间;
对象:类的变量,是具体的,占用内存空间;
#include
using namespace std;
class A{
public:
void set_val(int n)
{
//加一些限制一条件,按照用户的想法来做
if(n < 0 | n > 1000)
{
val = 0;
}
else{
val = n;
}
}
void print_val(void)
{
cout << "val = " << val << endl;
}
private:
int val;
};
int main()
{
A *p = new A;//p是对象指针 ,在堆区开辟空间,手动开辟,手动关闭
p->set_val(100);
p->print_val();
delete p;
return 0;
}
5.3构造函数和析构函数
每个类都有,若没有定义,系统会提供默认的
构造函数--初始化操作,定义对象时执行(自动)
函数名与雷鸣相同的函数,构造函数没有返回值,不能直接调用
析构函数--回收资源,对象销毁时执行(自动),用来做清理工作
函数名与雷鸣相同的前面~,析构函数没有返回值也没有参数,不能直接的调用
定义析构函数和构造函数:
#include
using namespace std;
class A{
A()
{
//构造函数
}
~A()
{
//析构函数
}
};
int main()
{
return 0;
}
5.4this指针
this指针是一个特殊的指针,指向当前的对象;
如果要引用整个对象则*this
this指针仅能在类内部使用,能够提取类中的私有成员变量和函数
#include
using namespace std;
class A{
public:
//用this来区分形参和成员变量
void set_val(int val)
{
this->val = val;
}
void print_val(void)
{
cout << "val = " <print_val();
}
private:
int val;
};
int main()
{
A a;
a.set_val(100);
a.print_val();
cout << "&a=" << &a << endl;
a.test();
return 0;
}
以上是近期所学的部分知识点,写得不是很完善,争取下一次写得更好!!!