C/C++基础 (new, delete)

//class_2.h
#ifndef _CLASS_2_H
#define _CLASS_2_H
class Circle {
public:
	Circle() {
		printf("ccccc1\n");
		m_x = m_y = 0;
		m_radius = 0;	
	}
	Circle(int x,int y,int r) {
		printf("ccccc2\n");
		this->m_x = x;  //fz
		this->m_y = y;
		this->m_radius = r;
	}
	~Circle() {
		printf("dddddd\n");
	}
	int GetX() {
		return m_x;
	}
	void SetX(int x) {
		m_x = x;
	}
	void MoveTo(int x, int y) {
		m_x = x;
		m_y = y;
	}
	void SetRadius(int radius) {
		m_radius = radius;
	}
	double Area();
private:
	int m_x;
	int m_y;
	int m_radius;
};

class Object {
public:
	Object(): m_circle(1,2,3){ //csh
	
		printf("Object created!\n");
	}
	~Object()
	{
		printf("Object delected\n");
	}

private:
	Circle m_circle;
};

double Circle::Area() {
	return 3.14 * m_radius * m_radius;
}
#endif // !_CLASS_2_H
//class_main1.cpp
#include<stdio.h>

class Object
{
public:
	int x;
	int y;
	void Test(int x) {
		printf("x=%d,y=%d,Add=%d\n", this->x, this->y, this->Add())	
	}
private:
	int Add() {
		return x + y;
	}
};
int main() {
	Object a;
	a.x = 1;
	a.y = 2;
	a.Test(4);
}
//class_main2.cpp
#include<stdio.h>
#include<string>
#include"class_2.h"

int main() {
	Circle* n = (Circle*) malloc(sizeof(Circle)); // Circle not use gzhs to create object truely
	Circle obj; // Eorr: Circle obj();
	Circle obj0(1, 2, 3);
	obj.SetRadius(1);
	obj.MoveTo(0, 0);
	double aras = obj.Area();
	{
		Circle obj1(2, 2, 3);
		double aras1 = obj1.Area();
	}
	Object obj2;
	int* m = new int(0); // int* m = new int();  int* m = new int;
	int* p = new int[2];
	p[0] = 1;
	p[1] = 2;
	delete m;
	m = NULL;
	delete [] p;
	p = NULL;
	return 0;
}
// class_main3.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class Tutorial {
public:
	char name[32];
	char author[16];

public:
	Tutorial(char* n , char* a) {
		strcpy(name, n);
		strcpy(author, a);	
	}
	void ShowInfo() {
		printf("Tourial1: %s, %s\n",name ,author);
	}
	virtual void mybcx() {
	
		printf("miang yao bei chong xie!");
	}
	virtual void cxhs() = 0;
	virtual ~Tutorial() {} // must add virtual 
private:
	int iprivate;

protected:
	int iprotected;
};

class ViodeTuorial :public Tutorial {

public:
	ViodeTuorial() :url("www"), visits(1),Tutorial("C yu yan","wl") {   // Eorr: iprotected(100), name("mr")	
		iprotected = 100;
	}
	void Play() {
		printf("PLAY");
	}
public:
	char url[128];
	int visits;
public:
	void ShowInfo() {
		Tutorial::ShowInfo();
		printf("Tourial2: %s, %s\n", name, author);
	}
	void mybcx() {
		printf("zhi xing de shi wo!");
	}
	void cxhs() {}
};


int main() {
	ViodeTuorial cpp_guide;
	strcpy(cpp_guide.author,"shaofa");
	strcpy(cpp_guide.name,"C/C++ zhinan"); // include 00 -> 13
	// Eorr: cpp_guide.iprotected = 100;
	cpp_guide.ShowInfo();
	printf("%d,%d\n",sizeof(cpp_guide.name),strlen(cpp_guide.name)); // 32, 12
	Tutorial* p = new ViodeTuorial();
	p->mybcx();  // virtual , zhi xing child's function
	delete p; // first delete child's ~ ,because of virtual ~Tutorial() {}
}
// class_main4.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class Text {
	friend void my_print(Text* p);
	friend void my_print(Text& p);
	friend class Something;
public:
	Text(const char* str) {
		m_size = strlen(str) + 1;
		m_buf = new char[m_size];
		strcpy(m_buf,str);
	}
	Text(const Text& other) {
		m_size = other.m_size;
		m_buf = new char[m_size];
		strcpy(m_buf, other.m_buf);
	}
	~Text() {
		delete[] m_buf;
	}
	const char* GetText() {
		return m_buf;
	}
	char& operator[] (int index) {
		return m_buf[index];
	}

/*
private:
	Text(const Text& other) {}
*/
private:
	int m_size;
	char* m_buf;
};
void my_print(Text* p) {
	printf("%s",p->m_buf);
}
void my_print(Text& p) {
	printf("%s", p.m_buf);
}

class Something {
public:
	void my_print(Text& p) {
		printf("%s", p.m_buf);
	}
};
int main() {
	Text t1("hello,world!\n");
	Text t2(t1);
	my_print(&t1); 
	::my_print(t1); // my_print(t1);
	char& ch = t1[0];
	printf("%c\n",ch);
	ch = 'H';
	printf("%c\n", t1.GetText()[0]);
	Something s;
	s.my_print(t1);
}
//class_main5.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

class Fraction {
public:
	Fraction() :num(1), den(1) {}
	Fraction(int n, int d) :num(n), den(d){}
	Fraction operator + (const Fraction& other ) {   // jiu jing yuan ze ,diao yong zhe ge,qiang ji
		Fraction result;
		printf("bbbbb");
		result.den = den*other.den;
		result.num = num*other.den + den*other.num;
		return result;
	}
	bool operator == (const Fraction& other) {
		if (num*other.den == den*other.num) {
		
			return true;
		}
		return false;
	}
	operator double() {
		return (double)num / den;
	}

private: 
	int num;
	int den;

friend Fraction operator + (const Fraction& a, const Fraction& b);
};
Fraction operator + (const Fraction& a, const Fraction& b) {   // qiang ji,quanju czysf
	Fraction result;
	printf("ddd");
	result.den = a.den*b.den;
	result.num = a.num*b.den + a.den*b.num;
	return result;
}
int main() {
	Fraction fa(2, 3);
	Fraction fb(3, 5);
	Fraction fc = fa + fb;
	bool b = (fa == fb);
	double fa_d = (double)fa;
}
// class_main6.cpp
#include<stdio.h>
#include<string.h>
#include<stdio.h>

class Point {
public:
	Point() {
		printf("-");
	}
	Point(int a) :m_a(a) {
		printf("~");
	}
	int m_a;
};
int ss();

class Logger {
public:
	Logger(){}	
	Logger& operator << (int a) {
		printf("%d",a);
		ss();
		return *this;
	}
	Logger& operator << (const char* s) {
		nbn hh;
		hh.nbn_m = 4;
		printf("%d", hh.nbn_m);
		printf("%s", s);
		return *this;
	}
	Logger& operator << (const Point& point) {
		printf("%d", point.m_a);
		return *this;
	}
	class nbn {
	public:
		int nbn_m;
	};
};
int ss() {
	printf("333");
	return 2;
}

int main() {
	Logger lg;
	Point ll = Point(13);
	lg  << "hello"<< Point(12)<< "\n";
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值