这些都不会丢人了

一个空类A,sizeof(A)是多少?

答案是1
空类被实例化,编译器插进去的一个char ,使得这个class的不同实体(object)在内存中配置独一无二的地址。
也就是说这个char是用来标识类的不同对象的

typedef的使用

解释typedef void (*pfun)(void):
概念理解:typedef是对已有的类型进行别名定义,不产生新的类型;
#define 只是在预处理的过程中对代码进行简单的替换。
类比理解:typedef unsigned int UNT32;//UINT32类型就是unsigned int;
UINT32 sun;//定义一个变量:int sum;

typedef int arr[3];//arr类型是int[3];(存放int类型数据的数组)
arr a;//定义一个数组:int a[3];
typedef void(pfun)(void); //pfun类型是void()(void)
pfun main;//定义一个函数:void(*main)(void);

strcpy、strncpy、memcpy的区别

void *memcpy(void *s1, const void *s2, size_t n);
char *strcpy(char *s2, const char *s1);
char *strncpy(char *s2, const char *s1, size_t n);
参考:https://blog.csdn.net/taric_ma/article/details/7383713
1.strcpy不支持选定n个字节copy
2.strcpy、strncpy以/0为结束符,只能复制字符串
3.memcpy返回s1,其他返回s2

多态有几种

1.静态多态
重载
2.动态多态-覆盖
子类实现父类的虚函数

重定义/隐藏

  1. 如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual关键字,基类的函数将被隐藏(注意别与重载混淆)。

  2. 如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)。

几种调用

#include <iostream>
using namespace std;
class A
{
public:
	A(){
		cout<<"A"<<endl;
	}
 
	~A(){}
 
	virtual void func1()
	{
		cout<<"only in A"<<endl;
	}
	virtual void func2()
	{
		cout<<"virtual in A"<<endl;
	}
 
	void func3()
	{
		cout<<"non virtual in A"<<endl;
	}
};
 
class B:public A
{
public:
	B(){
		cout<<"B"<<endl;
	}
 
	~B(){}
 
	 void func2()
	{
		cout<<"virtual in B"<<endl;
	}
 
	 void func3()
	{
		cout<<"non virtual in B"<<endl;
	}
};
 
int main()
{
	B *b = new B();		//A B
	A *a = (A *)b;
	b->func1();			//only in A
	b->func2();			//virtual in B
	b->func3();			//non virtual in B
	a->func1();			//only in A
	a->func2();			//virtual in B
	a->func3();			//non virtual in A
	getchar();
	return 0;
}

继承方式

https://www.cnblogs.com/33debug/p/6666939.html
(1) 公有继承(public)

公有继承的特点是基类的公有成员和保护成员作为派生类的成员时,它们都保持原有的状态,而基类的私有成员仍然是私有的,不能被这个派生类的子类所访问。

(2)私有继承(private)

私有继承的特点是基类的公有成员和保护成员都作为派生类的私有成员,并且不能被这个派生类的子类所访问。

(3)保护继承(protected)

保护继承的特点是基类的所有公有成员和保护成员都成为派生类的保护成员,并且只能被它的派生类成员函数或友元访问,基类的私有成员仍然是私有的。

private能够对外部和子类保密,即除了成员所在的类本身可以访问之外,别的都不能直接访问。protected能够对外部保密,但允许子类直接访问这些成员。public、private和protected对成员数据或成员函数的保护程度可以用下表来描述:
  在这里插入图片描述

判断两个无环单链表是否有交点,如果有返回该节点

https://blog.csdn.net/jiary5201314/article/details/50990349

typedef struct node_t
{
	int data;//data
	struct node_t *next; //next
}node;
 
node* find_node(node *head1, node *head2)
{
	if(NULL == head1 || NULL == head2)
	{
		return NULL;//如果有为空的链表,肯定是不相交的
	}
	node *p1, *p2;
	p1 = head1;
	p2 = head2;
	int len1 = 0;
	int len2 =0;
	int diff = 0;
	while(NULL != p1->next)
	{
		p1 = p1->next;
		len1++;
	}
	while(NULL != p2->next)
	{
		p2 = p2->next;
		len2++;
	}
	if(p1 != p2) //如果最后一个节点不相同,返回NULL
	{
	return NULL;
	}
	diff = abs(len1 - len2);
	if(len1 > len2)
	{
		p1 = head1;
		p2 = head2;
	}
	else
	{
		p1 = head2;
		p2 = head1;
	}
	for(int i=0; i<diff; i++)
	{
		p1 = p1->next;
	}
	while(p1 != p2)
	{
		p1 = p1->next;
		p2 = p2->next;
	}
	return p1;
}

实现stack

https://www.cnblogs.com/whlook/p/6531760.html

template<class T>
struct node
{
    T value;  //储存的值
    node<T>* next; 

    node() :next(nullptr){} //构造函数
    node(T t) :value(t), next(nullptr){}
};
template<class T>
class myStack
{
    int cnts; //入栈数量
    node<T> *head; //栈的头部
public:

    myStack(){ cnts = 0; head = new node<T>; }
    void stackPush(T arg); //入栈
    T stackPop();  //出栈
    T stackTop(); //获取栈顶元素

    void printStack(); //打印栈
    int counts(); //获取栈内元素个数
    bool isEmpty(); //判断空
};
template<class T>
void myStack<T>::stackPush(T arg)
{
    node<T> *pnode = new node<T>(arg); //申请入栈元素的空间
    pnode->next = head->next;
    head->next = pnode;
    cnts++;
}
template<class T>
T myStack<T>::stackPop()
{
    if (head->next!=nullptr) 
    {
        node<T>* temp = head->next;
        head->next = head->next->next;
        T popVal = temp->value;
        delete temp;
        return popVal;
    }
}
template<class T>
T myStack<T>::stackTop()
{
    if (head->next!=nullptr)
    {
        return head->next->value;
    }
}
template<class T>
void myStack<T>::printStack()
{
    if (head->next != nullptr)
    {
        node<T>* temp = head;
        while (temp->next != nullptr)
        {
            temp = temp->next;
            cout << temp->value << endl;
        }
    }
}
template<class T>
int myStack<T>::counts()
{
    return cnts;
}
template<class T>
bool myStack<T>::isEmpty()
{
    if (cnts)
        return false;
    else
        return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值