C进阶

C进阶

二级指针+结构体

typedef struct {
    int age;
    char *name;
    char **stdname;
}Teacher;

int  createTeacher(Teacher **res,int num){
    Teacher *T=NULL;
    T=(Teacher * )malloc(sizeof(Teacher)*num);
    memset(T,0,sizeof(T));
    for(int i=0;i<num;i++){
        T[i].name=(char *)malloc(60);
        if(T[i].name==NULL){
            return -1;
        }
        T[i].stdname=(char **)malloc(sizeof(char*)*3);
        for(int k=0;k<3;k++){
            T[i].stdname[k]=(char *)malloc(60);
            if(T[i].stdname[k]==NULL){
                return -1;
            }
        }
    }
    for(int i=0;i<num;i++){
        printf("\nplease enter name:");
        scanf("%s",T[i].name);
        printf("\nplease enter age: ");
        scanf("%d",&(T[i].age));
        printf("\nplease enter std0name:");
        scanf("%s",T[i].stdname[0]);
        printf("\nplease enter std1name:");
        scanf("%s",T[i].stdname[1]);
        printf("\nplease enter std2name:");
        scanf("%s",T[i].stdname[2]);
    }
    *res=T;
    return 0;

}
void freeTeaher(Teacher * T,int num){
    for(int i=0;i<num;i++){
        if(T[i].name!=NULL){
            free(T[i].name);
            T[i].name=NULL;
        }
        if(T[i].stdname!=NULL){
            for(int j=0;j<3;j++){
                if(T[i].stdname[j]!=NULL){
                    free(T[i].stdname[j]);
                    T[i].stdname[j]=NULL;
                }
            }
            free(T[i].stdname);
            T[i].stdname=NULL;
        }

    }
    free(T);
}
int main(){
    Teacher *t=NULL;
    int num=1;
    int ret=createTeacher(&t,num);
    freeTeaher(t,num);
    int cc=1;
    //73

}

C++进阶

三目运算符

#include<iostream>
using namespace  std;
namespace Name{
	int a=10;

}
int main(){
	int a=10;
	int b=20;
	(a<b? a:b)=30;//c++中表达式返回的是变量本身,c语言中表达式返回的是变量的值
	//想当于 10=30;
	//C语言中如何实现C++的效果
	*(a<b? &a:&b)=30;
}

运算符重载

#include<iostream>
using namespace std;


class Point{
public:
Point(int x,int y){X=x;Y=y;}
~Point(){}
Point & operator ++();  //前置操作 
Point operator ++(int); //后置操作
Point & operator --();  //前置操作 
Point operator --(int); //后置操作 
int GetX(){return X;}
int GetY(){return Y;}
private:
int X,Y;
};
Point& Point::operator ++()
{
X++;
Y++;
return *this;
}
Point Point::operator ++(int)
{
Point temp=*this;
++(*this);
return temp;
}
Point& Point::operator --()
{
X--;
Y--;
return *this;
}
Point Point::operator --(int)
{
Point temp=*this;
--(*this);
return temp;
}
int main()
{
Point A(4,6);
cout<<"A的值为:"<<A.GetX()<<","<<A.GetY()<<endl;
A++;
cout<<"A的值为:"<<A.GetX()<<","<<A.GetY()<<endl;
++A;
cout<<"A的值为:"<<A.GetX()<<","<<A.GetY()<<endl;
A--;
cout<<"A的值为:"<<A.GetX()<<","<<A.GetY()<<endl;
--A;
cout<<"A的值为:"<<A.GetX()<<","<<A.GetY()<<endl;
return 0;
}
#include<iostream>
#include<cstring>
using namespace  std;
class Complex{
	public:
	int a;
	int b;
	Complex(int a=0,int b=0){
		this->a=a;
		this->b=b;
	}
	~Complex (){
		cout<<"deconstruct"<<endl;
	}
	friend Complex operator+ ( Complex & c1 ,Complex & c2);
	Complex& operator++(int){
		Complex tmp=* this;
		this->a++;
		this->b++;
		return tmp; 
	}
	Complex operator++(){
		this->a++;
		this->b++;
		return * this;
	}
	friend ostream & operator<<(ostream & out,Complex &c1);
	//complex & operator=(Complex &c1);
};
Complex operator+(Complex & c1,Complex & c2){
	return Complex (c1.a+c2.a,c1.b+c2.b);
}
ostream & operator<<(ostream & out,Complex & c1){
	out<<c1.a<<" "<<c1.b<<endl;
	return out;
}
int main(){
	Complex c1(1,2);
	Complex c2(3,5);
	Complex c3=c1+c2;
	++c1;
	c1++;
	cout<<c1<<c2<<endl;
}

MyArray.h

#pragma once
class MyArray
{	public:
		MyArray(int length);
		~MyArray();
		MyArray(const MyArray& obj);
		void setDate(int idx,int data);
		int getData(int idx);
		int getlength() const;
		int & operator[](int i);
		MyArray& operator=(MyArray & obj);
		bool operator==(MyArray &a1);
private:
	int m_len;
	int* m_data;
};

MyArray.cpp

#include<iostream>
#include "MyArray.h"

MyArray::MyArray(int length) {
	m_len = length;
	m_data = new int[length];

}
int MyArray::getlength()const {
	return m_len;
}
MyArray::MyArray(const MyArray& obj) {
	int len = obj.getlength();
	m_data = new int[len];
	this->m_len = len;
	for (int i = 0; i < len; i++) {
		this->m_data[i] = obj.m_data[i];
	}
}
MyArray::~MyArray() {
	if (m_data != NULL) {
		delete[]m_data;
		m_len = 0;
	}
}
void MyArray::setDate(int idx, int data) {
	m_data[idx] = data;
}
int MyArray::getData(int idx) {
	return m_data[idx];
}
int& MyArray::operator[](int i) {

	return m_data[i];
}
MyArray& MyArray::operator=(MyArray& obj) {
	m_len = obj.m_len;
	if (m_data != NULL) {
		delete[] m_data;
	}
	m_data = new int[m_len];
	for (int i = 0; i < m_len; i++) {
		m_data[i] = obj[i];
	}
	return *this;

}
bool MyArray::operator==(MyArray& a1) {
	if (this->m_len != a1.m_len) {
		return false;
	}
	for (int i = 0; i < m_len; i++) {
		if (m_data[i] != a1[i]) {
			return false;
		}
		return true;
	}
}

main.cpp

#include<iostream>
using namespace  std;
#include "iostream"
using namespace std;
#include"MyArray.h"

int main() {
	MyArray mya(10);
	for (int i = 0; i < 10; i++) {
		mya[i] = i;
	}
	for (int i = 0; i < 10; i++) {
		cout<<mya[i];
	}
	MyArray myb = mya;
	for (int i = 0; i < 10; i++) {
		cout << myb.getData(i);
	}
	MyArray a3(10);
	a3 = mya;
	for (int i = 0; i < 10; i++) {
		cout << a3[i];
	}
	bool res = (mya == a3);
	int cc = 1;
}

MyString类

MyString.cpp

#include "MyString.h"
MyString::MyString() {
    
    m_len = 0;
    m_p = new char[m_len + 1];
    strcpy(m_p, "");

}
MyString::MyString(int len) {

    if (len == 0) {
        m_len = 0;
        m_p = new char[m_len + 1];
        strcpy(m_p, "");
    }
    else {
        m_len = len;
        m_p = new char[m_len + 1];
        memset(m_p, 0, m_len);
    }
    
}
MyString::MyString(const char* p) {
    if (p == NULL) {
        m_len = 0;
        m_p = new char[m_len + 1];
        strcpy(m_p, "");
    }
    else {
        m_len = strlen(p);
        m_p = new char[m_len + 1];
        strcpy(m_p, p);
    }
}
MyString::MyString(const MyString& s) {
    m_len = s.m_len;
    m_p = new char[m_len + 1];
    strcpy(m_p, s.m_p);

}
MyString::~MyString() {
    if (m_p != NULL) {
        delete[] m_p;
        m_p = NULL;
        m_len = 0;
    }
}
MyString& MyString:: operator=(const MyString& obj) {
    if (this->m_p != NULL) {
        delete[] m_p;
        m_len = 0;
    }
    m_len = obj.m_len;
    m_p = new char[m_len + 1];
    strcpy(m_p, obj.m_p);
    return *this;
}
MyString& MyString::operator=(const char* p) {
    if (this->m_p != NULL) {
        delete [] m_p;
        m_len = 0;
    }
    if (p == NULL) {
        m_len = 0;
        m_p = new char[m_len + 1];
        strcpy(m_p, "");
    }
    else {
        m_len = strlen(p);
        m_p = new char[m_len+1];
        strcpy(m_p, p);
    }
    return *this;

}
char& MyString::operator[](int idx) {
    
    return m_p[idx];
}
ostream& operator<<(ostream& out, MyString& s) {
    out << s.m_p;
    return out;
}
bool MyString::operator==(const MyString& obj) {
    if (m_len != obj.m_len) {
        return false;
    }
    return !strcmp(m_p, obj.m_p);
}
bool MyString::operator==(const char* p) {
    if (p == NULL) {
        if (m_len == 0) {
            return true;
        }
        else {
            return false;
        }
    }
    else {
        if (m_len == strlen(p)) {
            return !strcmp(m_p, p);
        }
        else {
            return false;
        }
    }
}
bool MyString::operator!=(const MyString& obj) {
    return !(*this == obj);
}
bool MyString::operator!=(const char* p) {
    return !(*this == p);
}
int MyString::operator<(const char* p) const {
    return strcmp(this->m_p, p);
}
int MyString::operator<(const MyString& s) const {
    return strcmp(this->m_p, s.m_p);
}
int MyString::operator>(const char* p)const {
    return strcmp( p,this->m_p);
}
int MyString::operator>(const MyString& s) const {
    return strcmp(s.m_p,m_p);
}
istream& operator>>(istream& in, MyString& s) {
    cin >> s.m_p;
    return in;
}

MyString.h

#pragma once
#include<iostream>
using namespace std;
class MyString
{
public:
    MyString();
    MyString(int len);
    MyString(const char* p);
    MyString(const MyString& s);
    ~MyString();
    MyString& operator=(const MyString & obj);
    MyString& operator=(const char *p);
    char& operator[](int idx);
    friend ostream& operator<<(ostream & out, MyString & s);
    bool operator==(const MyString & obj);
    bool operator==(const char* p);
    bool operator!=(const MyString& obj);
    bool operator!=(const char* p);

    int operator<(const char *p) const;
    int operator<(const MyString& s) const;
    int operator>(const char *p)const;
    int operator>(const MyString& s) const;
    //把类的指针露出来
    char* c_str() {
        return m_p;
    }
    const char* c_str2() {
        return m_p;
    }
    int length() {
        return m_len;
    }
    friend istream& operator>>(istream & in,MyString & s);
private:
    int m_len;
    char* m_p;
};

main.cpp

#include<iostream>
using namespace  std;
#include"MyString.h"
int main01() {
	MyString s1;
	MyString s2("s2");
	MyString s2_2 = NULL;
	MyString s4 = s2;
	s4 = s2;
	s4 = "2222";
	s4[1] = '3';
	cout << s4;
	int cc = 1;
	return 0;
}
int main() {
	MyString s1;
	MyString s2("s2");
	MyString s3=s2;
	bool res2 = (s2 == "s2");
	bool res = (s2 == s3);
	int res1 = (s2 < "bbb");
	int res3 = (s2 < s3);
	MyString s4 = "123";
	strcpy(s4.c_str(), "124");

	MyString s6(128);
	cin >> s6;
	int cc = 1;
}

编写通讯协议
CSocketProtocol.h

#pragma once
#include<iostream>
using namespace std;
class CSocketProtocol
{
public:
	CSocketProtocol();
	virtual int cltSocketInit() = 0;
	virtual int clsSocketSend( char* buf, int buflen) = 0;
	virtual int cltSocketRev( char* buf, int* buflen) = 0;
	virtual int cltSocketDestroy() = 0;
	virtual ~CSocketProtocol() {  };//虚析构函数的细节
protected:
private:

};

CSocketFactoryImp1.h

#pragma once
#include<iostream>
using namespace std;
#include"CSocketProtocol.h"
class CSocketFactoryImp1:public CSocketProtocol
{
public:
	virtual int cltSocketInit() ;
	virtual int clsSocketSend(char* buf, int buflen) ;
	virtual int cltSocketRev(char* buf, int* buflen) ;
	virtual int cltSocketDestroy();
protected:
private:
	char* p;
	int len;
};

CSocketFactoryImp1.cpp

#include "CSocketFactoryImp1.h"
#include"CSocketProtocol.h"


int CSocketFactoryImp1::cltSocketInit() {
	p = NULL;
	len = 0;

	return 0;
 }
int CSocketFactoryImp1::clsSocketSend(char* buf, int buflen) {
	p = (char*)malloc(sizeof(char) * buflen);
	if (p == NULL) {
		return -1;
	}
	memcpy(p, buf, buflen);
	len = buflen;
	return 0;
 }
int CSocketFactoryImp1::cltSocketRev(char* buf, int* buflen) {
	if (buf == NULL || buflen == NULL) {
		return -1;
	}
	*buflen = this->len;
	memcpy(buf, this->p, this->len);
	return 0;
 }
int CSocketFactoryImp1::cltSocketDestroy() {
	if (p!= NULL) {
		free(p);
		p = NULL;
		len = 0;
	}
	return 0;
 }

main.cpp

#include<iostream>
using namespace  std;
#include"CSocketProtocol.h"
#include"CSocketFactoryImp1.h"

int SckSendAndRec(CSocketProtocol * sp, char * in,int inlen, char * out,int *outlen) {
	int ret = 0;
	ret = sp->cltSocketInit();
	if (ret != 0) {
		goto END;
	}
	ret = sp->clsSocketSend(in, inlen);
	ret = sp->cltSocketRev(out, outlen);
	
END:
	ret = sp->cltSocketDestroy();
	return 0;
}
int main() {
	int ret = 0;
	//sp=new CSckFactoryImpl;
	char in[4096];
	int inlen;
	char out[4096];
	int outlen=0;
	strcpy((char*)in,"aasdsddfdgfgfhghghg");
	strcpy(in,"aaaaaaabbbbaaaaaaaaaa");
	inlen = 9;
	CSocketProtocol* sp = NULL;
	sp = new CSocketFactoryImp1;
	ret=SckSendAndRec(sp, in, inlen, out, &outlen);
	if (ret!= 0) {
		cout << "error" << endl;
		return ret;
	}
	delete sp;
	int cc = 1;
}

模板类派生

#include<iostream>
using namespace  std;
template <class T>
class A {
public:
	A(T t) {
		this->t = t;
	}
	T getA() {

		return this->t;
	}
	void printA() {
		cout << "t" << this->t << endl;
	}
private:
	T t;

};

class B :public A<int> {
public:
	B(int a, int b1):A<int>(a),b(b1){}
private:
	int b;
};
int main() {
	A<int> a1(5);
	B b(5, 3);

	int cc = 1;
	cout << "hello" << endl;
	return 0;

}

模板类继承模板类

#include<iostream>
using namespace  std;
template <class T>
class A {
public:
	A(T t) {
		this->t = t;
	}
	T getA() {

		return this->t;
	}
	void printA() {
		cout << "t" << this->t << endl;
	}
private:
	T t;

};

class B :public A<int> {
public:
	B(int a, int b1):A<int>(a),b(b1){}
private:
	int b;
};
template <class T>
class C :public A<T> {
public:
	C(T c, T a) :A<T>(a) {
		this->c = c;
	}
	void printC() {
		cout << "c:" << c << endl;
	}
protected:
	T c;
};
int main() {
	A<int> a1(5);
	B b(5, 3);
	C<int> c(3, 5);
	c.printC();
	int cc = 1;
	cout << "hello" << endl;
	return 0;

}

单个文件中的类模板

#include<iostream>
using namespace  std;
template <class T>
class Complex {

public:
	Complex(T a, T b);
	Complex operator +(Complex& c2);
	void printCom(); 
	//<T>可加可不加
	friend ostream& operator<<<T>(ostream& out, Complex& c3);
protected:

private:
	T a;
	T b;

};
template <class T>
Complex<T>::Complex(T a, T b) {
	this->a = a;
	this->b = b;
}
template <class T>
void Complex<T>::printCom() {
	cout << "a:" << a << "b" << b << endl;
}
template <class T>
ostream& operator<<(ostream& out, Complex<T>& c3) {
	out << "a:" << c3.a << "b" << c3.b << endl;
}
template <class T>
Complex<T>  Complex<T>::operator +(Complex<T>& c2) {
	Complex tmp(a + c2.a, b + c2.b);
	return tmp;
}
int main() {
	Complex<int> c1(3, 2);
	int cc = 1;
	return 0;
}

多文件模板类

Complexes.h

#pragma once
#include<iostream>
using namespace std;

template <class T>
class Complexes
{	public:
	Complexes(T a, T b);
	Complexes operator +(Complexes& c2);
	void printCom();
	//<T>可加可不加
	friend ostream& operator<<<T>(ostream& out, Complexes& c3);
private:
	T a;
	T b;
};

Complexes.hpp

#include "Complexes.h"


template <class T>
Complexes<T>::Complexes(T a, T b) {
	this->a = a;
	this->b = b;
}
template <class T>
void Complexes<T>::printCom() {
	cout << "a:" <<a << "b" << b << endl;
}
template <class T>
ostream& operator<<(ostream& out, Complexes<T>& c3) {
	out << "a:" << c3.a << "b" << c3.b << endl;
}
template <class T>
Complexes<T>  Complexes<T>::operator +(Complexes<T>& c2) {
	Complexes tmp(a + c2.a, b + c2.b);
	return tmp;
}

main.cpp

#include<iostream>
using namespace  std;
#include"Complexes.hpp";


int main() {
	Complexes<int> c1(3, 2);
	int cc = 1;
	return 0;

}

自定义vector

注意每次先 在这里插入图片描述

MyVector.hpp

#include "MyVector.h"

template<class T>
MyVector<T>::MyVector(int size) {
	m_space = new T[size];
	m_len = size;
}
template<class T>
MyVector<T>::MyVector(const MyVector<T>& obj) {
	m_len = obj.m_len;
	m_space = new T[m_len];
	for (int i = 0; i < m_len; i++) {
		m_space[i] = obj.m_space[i];
	}
	
}
template<class T>
MyVector<T>::~MyVector() {
	if (m_space != NULL) {
		delete[]m_space;
		m_space = NULL;
		m_len = 0;
	}
}
template<class T>
T& MyVector<T>:: operator[](int index) {
	return m_space[index];
}
template<class T>
MyVector<T>& MyVector<T> ::operator=(const MyVector<T>& obj) {
	if (m_space != NULL) {
		delete[] m_space;
		m_space = NULL;
		m_len = 0;
	}
	m_len = obj.m_len;
	m_space = new T[m_len];
	for (int i = 0; i < m_len; i++) {
		m_space[i] = obj.m_space[i];
	}
	return *this;
}
template < class T>
ostream& operator<<(ostream& out, MyVector<T>& obj) {
	for (int i = 0; i < obj.m_len; i++) {
		out << obj.m_space[i] << " ";
	}
	out << endl;
	return out;
}

MyVector.h

#pragma once
#include<iostream>
using namespace std;
template < class T>
class MyVector
{	public:
	MyVector( int size); 

	MyVector(const MyVector& obj);
	~MyVector();
	T& operator[](int index);
	MyVector& operator=(const MyVector& obj);
	int getlen() {
		return m_len;
	}
	friend ostream& operator<<(ostream& out, MyVector& obj);
private:
	int* m_space;
	int m_len;
protected:
};

main.cpp

#include"MyVector.hpp"

int main() {
	int a = 1;
	MyVector<int> myv1(10);
	for (int i = 0; i < myv1.getlen(); i++) {
		myv1[i] = i + 1;
		cout << myv1[i] << " ";
	}
	cout << endl;
	MyVector <int> myv2 = myv1; 
	for (int i = 0; i < myv2.getlen(); i++) {
		myv2[i] = i + 1;
		cout << myv2[i] << " ";
	}
	cout << endl;
	cout<<myv1;
	int cc = 1;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值