C 语言

关注公众号 jb51net

关闭
首页 > 软件编程 > C 语言 > C++核心编程之类和对象

C++核心编程之类和对象详解

作者:Gigi Princess

这篇文章主要介绍了C++编程进阶之类和对象用法等相关使用技巧,需要的朋友可以参考下,希望能够给你带来帮助

C++面向对象三大特性:封装、继承、多态

1.封装

1.1封装的意义

封装意义一:

语法:class 类名{ 访问权限: 属性 / 行为 };

//实例1
#define _CRT_SECURE_NO_WARNINGS 1
//求圆的周长
#include<iostream>
using namespace std;
#define pi 3.14
class circle
{
public:
	int m_d;//直径
	double calculate()
	{
		return pi*m_d;
	}
};
int main()
{
	circle c1;
	c1.m_d = 10;
	cout << "圆周长为" << c1.calculate() << endl;
	system("pause");
	return 0;
}
//实例2
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include <string>
class Student
{
public:
	string m_name;
	int m_num;
	void showStudent()
	{
		cout << " 姓名:" << "\t" << m_name << " 学号:" << m_num << endl;
	}
	void setname(string name)
	{
		m_name = name;
	}
	void setnum(int num)
	{
		m_num = num;
	}
};
int main()
{
	Student s1;
	s1.setname("zhangsan");
	s1.setnum(2001);
	s1.showStudent();
	Student s2;
	s2.setname("lisi");
	s2.setnum(2002);
	s2.showStudent();
    system("pause");
	return 0;
}

总结:类中的 属性行为 统一称为 成员

类中的 属性 又称为 成员属性 成员变量

类中的 行为 又称为 成员函数 成员方法

封装意义二

访问权限有三种

公共权限: public 成员类内可以访问,类外可以访问

保护权限: protected 成员类内可以访问,类外不可以访问 儿子可以访问父亲中的保护内容

私有权限: private 成员类内可以访问,类外不可以访问 儿子不可以访问父亲中的保护内容

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include <string>
class Student
{
public:
	string m_name;
protected:
	int m_num;
private:
	int m_age;
public:
	void func()
	{
		m_name = "lili";
		m_num = 1;
		m_age = 30;
	}
private:
	void func1()
	{
		m_name = "lili";
		m_num = 1;
		m_age = 30;
	}
};
int main()
{
	Student s1;
	s1.m_name = "lilllll";
	//s1.m_num = 2;//不可访问
	//s1.m_age = 20;//不可访问
	s1.func();
	//s1.func1();//不可访问
    system("pause");
	return 0;
}

1.2 struct 和 class 区别

在C++中 struct 和 class 区别在于 默认的访问权限不同

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class C1
{
	string m_a;
};
struct C2
{
	string m_b;
};
int main()
{
	//C1 c1;
	//c1.m_a = "lili";//不可访问
	C2 c2;
	c2.m_b = "lili";
    system("pause");
	return 0;
}

1.3 成员属性设置为私有

优点:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include <string>
using namespace std;
class C1
{
public:
	void setname(string name)
	{
		m_name = name;
	}
	string getname()
	{
		return m_name;
	}
	int getnum()
	{
		return m_num;
	}
	void setage(int age)
	{
		m_age = age;
	}
private:
	string m_name;
	int m_num;
	int m_age;
};
int main()
{
	C1 c;
	c.setname("yangyang");//可写
	cout << c.getname() << endl;//可读
	cout << c.getnum() << endl;//只读
	c.setage(30);//只写
    system("pause");
	return 0;
}

好的,到这里封装就算是学到手了,可以进行两个小案例:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include <string>
using namespace std;
class Cube
{
public:
	void setchang(int chang)
	{
		m_c = chang;
	}
	void setkuan(int kuan)
	{
		m_k = kuan;
	}
	void setgao(int gao)
	{
		m_g = gao;
	}
	int getchang()
	{
		return m_c;
	}
	int getkuan()
	{
		return m_k;
	}
	int getgao()
	{
		return m_g;
	}
	int S()
	{
		return 2 * (m_c*m_g + m_c*m_k + m_g*m_k);
	}
	int V()
	{
		return m_c*m_g*m_k;
	}
	bool isSamebyclass(Cube &c)
	{
		if (m_c == c.getchang() && m_g == c.getgao() && m_k == c.getkuan())
		{
			return true;
		}
		return false;
	}
private:
	int m_c;
	int m_k;
	int m_g;
};
bool isSame1(Cube &c1, Cube &c2)
{
	if (c1.getchang() == c2.getchang() && c1.getgao() == c2.getgao() && c1.getkuan() == c2.getkuan())
	{
		return true;
	}
	return false;
}
int main()
{
	Cube c1;
	c1.setchang(10);
	c1.setkuan(10);
	c1.setgao(10);
	cout << "面积:" << c1.S() << endl;
	cout << "体积:" << c1.V() << endl;
	Cube c2;
	c2.setchang(11);
	c2.setkuan(11);
	c2.setgao(11);
	cout << "面积:" << c2.S() << endl;
	cout << "体积:" << c2.V() << endl;
	bool ret = isSame1(c1, c2);
	if (ret)
	{
		cout << "全局函数判断:两个立方体相等" << endl;
	}
	else
	{
        cout << "全局函数判断:两个立方体不相等" << endl;
	}
	ret = c1.isSamebyclass(c2);
	if (ret)
	{
		cout << "成员函数判断:两个立方体相等" << endl;
	}
	else
	{
		cout << "成员函数判断:两个立方体不相等" << endl;
	}
	system("pause");
	return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include <string>
using namespace std;
class Point
{
public:
	void setX(int x)
	{
		m_x = x;
	}
	int getX()
	{
		return m_x;
	}
	void setY(int y)
	{
		m_y = y;
	}
	int getY()
	{
		return m_y;
	}
private:
	int m_x;
	int m_y;
};
class Circle
{
public:
	void setR(int r)
	{
		m_r = r;
	}
	int getR()
	{
		return m_r;
	}
	void setcenter(Point center)
	{
		m_center = center;
	}
	Point getcenter()
	{
		return m_center;
	}
private:
	int m_r;
	Point m_center;
};
void C_and_P(Circle &c, Point &p)
{
	int distance = (c.getcenter().getX() - p.getX())*(c.getcenter().getX() - p.getX()) + (c.getcenter().getY() - p.getY())* (c.getcenter().getY() - p.getY());
	int doubleR = c.getR()*c.getR();
	if (distance==doubleR)
	{
		cout << "点在圆上" << endl;
	}
	else if (distance<doubleR)
	{
		cout << "点在圆内" << endl;
	}
	else
	{
		cout << "点在圆外" << endl;
	}
}
int main()
{
	Point p;
	p.setX(10);
	p.setY(30);
	Circle c;
	c.setR(10);
	Point center;
	center.setX(26);
	center.setY(43);
	C_and_P(c, p);
    system("pause");
	return 0;
}

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

您可能感兴趣的文章:
阅读全文