C/C++构造函数

1、在类中定义构造函数

#include <stdio.h>
#include <string.h>

class Student
{
private:
	int id;
	char name[32];

public:
	Student(int id, const char* name)
	{
		this->id = id;
		strcpy(this->name, name);
	}
};

int main()
{
	Student  s ( 201601, "shaofa");
	return 0;
}

①构造函数时,需要加上public:,类元素都是private。构造函数+(参数)即可,不需要重新定义一个新的函数名

②关于字符串的复制,用函数复制字符串。不能直接复制

③(参数),形参的命名直接采用类中元素名,字符串用char* name

④this->id=id,形参赋值到类元素

 

2、在类的构造函数对类的元素赋值

#include <stdio.h>
#include <string.h>

class Student
{
private:
	int id;
	char name[32];

public:
	Student()
	{
		id = 0;
		name[0] = 0;
	}
};

int main()
{
	Student  s ;
	return 0;
}

 直接在{}中赋值(自己错误的在()中赋值)

注意:类对应的{}后是加了;号,里面定义public和private时,{}后面是没有;的

 

3、重载构造函数

#include <stdio.h>
#include <string.h>

class Student
{
private:
	int id;
	char name[32];

public:
	// 默认构造函数
	Student()
	{
		id = 0;
		name[0] = 0;
	}

	//  带参构造函数
	Student(int id, const char* name)
	{
		this->id = id;
		strcpy(this->name, name);
	}
};

int main()
{
	Student  s1 ;
	Student  s2 ( 201601, "shaofa");
	return 0;
}

默认构造函数:不传入参数,直接赋值为0

带参数的构造函数:传入形参后进行赋值

 

4、当构造的函数与类名不相同时

#include <stdio.h>
#include <string.h>

class Triangle
{
	// 成员变量
	// 成员函数
private:
	int a, b, c;

public:
	Triangle(int a, int b, int c): a(a), b(b), c(c)
	{
	}

	// 判断是否为直角三角形
	bool right()
	{
		int aa = a * a;
		int bb = b * b;
		int cc = c * c;
		if(aa+bb == cc || aa+cc==bb || bb+cc==aa)
		{
			return true;
		}
		return false;
	}
};

int main()
{
	Triangle t(3,4,6);
	if(t.right())
	{
		printf("是直角三角形!\n");
	}
	else
	{
		printf("不是直角三角形!\n")
	}
	return 0;
}

在类中: 先定义构造函数,再定义成员函数

在main中:先定义对象t,再通过t.right()调用类中的成员函数

 

重点题目:

1、求两点之间的距离

#include <stdio.h>
#include <math.h>

class AfPoint
{
private:
	int x, y;

public:
	AfPoint()
		: x(0), y(0)
	{

	}
	AfPoint(int x, int y) 
		: x(x), y(y)
	{
	}
public:
	double distance(const AfPoint& other)
	{
		int dx = x - other.x;
		int dy = y - other.y;
		return sqrt( double(dx*dx + dy*dy));
	}
};


int main()
{
	AfPoint p1(0,0);
	AfPoint p2(10,10);

	double d = p1.distance(p2);
	printf("distance: %.2lf\n", d);
	return 0;
}

 定义新的函数来求坐标差dx,传入参数为const AFpoint& other,通过other.x传入另一个坐标

mian中通过p1.distance(p2)实现求坐标差

 

类定义和main中参数传入

#include <stdio.h>

class AfInteger
{
private:
	int value;

public:
	AfInteger()
	{
		this->value = 0;
	}
	AfInteger(int value):value(value)
	{		
	}

	int getValue()
	{
		return value;
	}
};


int main()
{
	AfInteger obj = 12;
	printf("value: %d \n", obj.getValue());
	return 0;
}

mian中调用类中函数,则在类中需要在成员函数中return 变量

在默认构造函数中给变量赋初值

直接将值赋给创建的对象,此值会通过带参数的构造函数传入给value吗?

 

每一个类中都需要定义一个默认的构造函数,默认构造函数一定是用来赋初值的(两种赋初值的方法,另一种for循环赋值方法如下题)

在一个类的成员函数中传入另一个类:参数的使用方法

#include <stdio.h>

class AfPoint
{
public:
	AfPoint():x(0), y(0) 
	{}
	AfPoint(int x, int y):x(x), y(y) 
	{}
	int x;
	int y;
};
class AfSize
{
public:
	AfSize():width(0), height(0) 
	{}
	AfSize(int w, int h):width(0), height(0) 
	{}
	int width;
	int height;
};
class AfRect
{
private:
	AfPoint location;
	AfSize size;

public:
	AfRect()
	{
	}
	AfRect(int x, int y, int width, int height)
		: location(x,y), size(width, height)
	{

	}
	AfRect(const AfPoint& pt, const AfSize& size)		
	{
		location = pt;
		size = size;
	}
};

int main()
{
	AfRect r1 (0,0, 640, 480);
	AfRect r2 (AfPoint(0,0), AfSize(640, 480)); // 使用临时对象,语法见书本内描述

	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值