- 类型转换运算符
C语言:隐式类型转换(char/int/long/float/double)
强制类型转换
(不安全,任何类型都可以互相转换,不做类型检查)
int num = 5;
double b = num;
num = 'a';
int *p = #
char *ptr = (char *)p;//强制类型转化
C++:
static_cast(相关类型转化:void *和其他类型指针之间的转化 父类和子类之间的转化)
int num = 5;
double b = static_cast<double>(num);
char ch = static_cast<char>(num);
int *p_num = #
void *p = p_num;
char *ptr = static_cast<char *>(p);
const_cast(去除指针和引用的const属性)
reinterpret_cast(可以将任意类型指针做转换)(类似于C的强制类型转换,不安全)
dynamic_cast(多态)
- C++封装
定义:类和对象实现封装
作用:提高代码的维护性 保证代码的独立性(高内聚低耦合),对外提供接口,屏蔽数据,对内开放数据,
C语言的封装:当单一变量无法完成描述需求的时候,结构体类型解决了这一问题
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedf Node * Link;
void init_link(Link *head)
{
*head = NULL;
}
void insert_head_node(Link *newnode, Link *head)
{
(+newnode)->next = *head;
*head = *newnode;
}
int display_link(Link *head)
{
if(*head == NULL)
{
return 0;
}
Link temp = *head;
while(temp != NULL)
{
printf("%d\n",temp->num);
temp = temp->next;
}
}
int main()
{
Link head;
Link newnode;
init_link(&head);
for(int i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_head_node(&newnode,&head);
}
display_link(&head);
return 0;
}
link.h
#pragma once
struct node
{
int num;
struct node *next;
};
class Link
{
public:
void initLink()
void insertHeadNode(node *newnode);
//void insertTailNode(node *newnode);
//void insertModNode(node *newnode,int num);
//void deleteNode(node *newnode);
void displayLink();
node * getHead();
private:
node *head;
};
link.cpp
#include <stdio.h>
#include "link.h"
using namespace std;
void Link::initLink()
{
head =NULL;//NULL nullptr
}
void Link::insertHeadNode(node *newnode)
{
newnode->next = head;
head = newnode;
}
void Link::displayLink()
{
node * temp = head;
while(temp !=NULL)
{
printf("temp =%d\n",temp->num);
temp = temp->next;
}
}
main.cpp
#include <iosream>
#include "link.h"
using namespace std;
int main()
{
Link link;
link.initLink();
for(int i =0; i<10;i++)
{
node *newnode = new node;
newnode->num = i+1;
link.insertHeadNode(newnode);
}
link.displayLink();
return 0;
}
class.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
class Cal
{
public:
double getResult();
void setNum1(double a);
void setNum2(double b);
void setOp(string op);
private:
double num1;
double num2;
string op;
};
class.cpp
#include"cal.h"
void Cal::setNum1(double a)
{
num1= a;
}
void Cal::setNum2(double b)
{
num2 = b;
}
void Cal::setOp(string op)//setOp(string op, Cal *this)
{
op = op;//this指针
}
double Cal::getResult()
{
if(op == "+")
{
return num1+num2;
}
else if(op == "-")
{
return num1 - num2;
}
}
计算器(加和减)
#include <iosream>
#include "link.h"
#include "cal.h"
using namespace std;
int main()
{
Cal cal;
cal,setNumber1(1.0);//cal.setNum1(1.0,&cal)
cal.setNumber2(2.0);
cal.setOp("+");
double result = cal.getResult();
cout << result <<endl;
return 0;
}
类的声明
this指针:
#include <iostream>
using namespace std;
class Test
{
public:
int a;
int b;
void print()
{
cout << a << " " << b << endl;
}
};
int main()
{
Test t;
Test t1;
cout << sizeof(t) << endl;
cout << sizeof(t1) << endl;
return 0;
}
-
this指针
为什么需要this指针
因为同一个类实力的对象共享代码段(方法),为了区分访问那个对象的成员,所以编译器会在编译阶段给类的所有方法加上this指针参数,用来指向当前操作的对象 -
构造函数
作用:用来初始化对象成员的值 引入作用:防止忘记初始化
特点:
1、函数名与类名相同
2、函数没有返回值
3、可以重载
4、可以自动调用
种类:
1、默认无参构造函数
2、有参构造函数
3、拷贝构造函数
4、类型转换构造函数 -
析构函数
作用:释放对象成员所分配的内存空间
特点:函数名是~类名,无参(不可重载,无返回值),当对象被释放,会先调用析构函数
引入目的:防止内存泄漏