前言:
面向对象三大特性:
封装,继承,多态。
万物皆是对象,对象上有属性和行为。
比如:
人是对象,手是属性,用手做饭是行为。
封装:
一.封装的意义。
a.将属性和行为做一个整体,表现生活中的事物。
b.将属性和行为加以权限控制。
a将属性和行为做一个整体,表现生活中的事物
分三部分
1.权限
2.属性
3.行为
//类中的属性和行为,统称为成员
//属性:成员属性,成员变量
//行为:成员函数,成员方法
语法:
class 某某
{
权限:
属性(变量)
行为(函数)
};
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
class Student
{
//访问权限
//公共权限
public:
//类中的属性和行为,统称为成员
//属性:成员属性,成员变量
//行为:成员函数,成员方法
//属性,用变量
//信息
string name;
string id;
//行为,用函数
//输出信息
void showmessage()
{
cout << name << endl;
cout << id << endl;
}
//或者你可以通过行为进行赋值
void setname(string names)
{
name = names;
}
void setid(string ids)
{
id = ids;
}
};
int main()
{
Student ji; //通过类,创建对象。
//实例化
ji.name = "阿吉"; //属性赋值
ji.id = "1112345";
ji.setname("aji");
ji.setid ("1");
ji.showmessage();
system("pause");
return 0;
}
b.将属性和行为加以权限控制
权限有三种:
//公共权限类外可以访问
//保护权限类外不可访问,在继承中,儿子可以访问父亲
//私有权限类外不可访问,在继承中,儿子不可以访问父亲
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
class Student
{
public:
string name;
protected:
string id;
private:
void showmessage()
{
cout << name << endl;
cout << id << endl;
}
};
int main()
{
Student j;
j.name = "das dsa"; //公共权限类外可以访问
j.id; //保护权限类外不可访问
j.showmessage(); //私有权限类外不可访问
system("pause");
return 0;
}
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
class Student
{
public:
//可读可写name
void setname(string m_name)
{
name = m_name;
}
string showname()
{
return name;
}
//只写Lover
void setlover(string m_Age)
{
lover = m_Age;
}
//只写age
void setsge(int aage)
{
if (aage > 100)
{
cout << "有毒" << endl;
age = 0;
return;
}
age = aage;
return;
}
int showage()
{
return age;
}
private:
string name;
int age;
string lover;
};
int main()
{
Student c1;
c1.setlover("貂蝉");
//cout<<c1.lover//不可访问
c1.setname("li");
cout << "name" << c1.showname() << endl;
c1.setsge(12);
cout << "nianling" << c1.showage() << endl;
c1.setsge(1002);
cout << "nianling" << c1.showage() << endl;
system("pause");
return 0;
}
实例1:
创建立方体类,并分别用成员函数和全局函数判断 是否相等:
#include<iostream>
using namespace std;
#include<string>
#include <cstdlib>//解决system不明确问题。
class Cube
{
public:
void set_l(int l)
{
m_l = l;
}
int show_l()
{
return m_l;
}
void set_w(int w)
{
m_w = w;
}
int show_w()
{
return m_w;
}
void set_h(int h)
{
m_h = h;
}
int show_h()
{
return m_h;
}
int calc_V()
{
return m_l * m_h * m_h;
}
bool chengyuan(Cube &s1)//防止赋值内存浪费空间
{
if (s1.show_h() == m_h && s1.show_l() == m_l && s1.show_w() == m_w)
{
return true;
}
return false;
}
private:
int m_l;
int m_w;
int m_h;
};
//全局函数判断相等
bool panduan(Cube s1, Cube s2)
{
if (s1.show_h() == s2.show_h() && s1.show_l()== s2.show_l() && s1.show_w() == s2.show_w())
{
return true;
}
return false;
}
int main()
{
Cube S1;
S1.set_h(10);
S1.set_l(10);
S1.set_w(10);
cout << "V是" << S1.calc_V() << endl;
Cube S2;
S2.set_h(10);
S2.set_l(10);
S2.set_w(10);
//全局函数
bool ret=panduan(S1, S2);
if (ret)
{
cout << "相等";
}
else
{
cout << "不等";
}
//成员函数
cout << endl << "成员" << endl;
ret =S1.chengyuan(S2);
if (ret)
{
cout << "相等";
}
else
{
cout << "不等";
}
system("pause");
return 0;
}
实例2:
设计一个判断点与园关系的程序:
第一点: 在类中一个类可以调用另一个类。
第二点:程序封装。
#include<iostream>
using namespace std;
#include<string>
#include<Math.h>
#include <cstdlib>//解决system不明确问题。
class Point
{
public:
void set_x(int x)
{
p_x = x;
}
int get_x()
{
return p_x;
}
void set_y(int y)
{
p_y = y;
}
int get_y()
{
return p_y;
}
private:
int p_x;
int p_y;
};
class Circle
{
public:
void set_r(int r)
{
m_r = r;
}
int get_r()
{
return m_r;
}
void set_point(Point x)
{
m_center = x;
}
Point get_point()
{
return m_center;
}
private:
int m_r;
Point m_center;//第一点: 在类中一个类可以调用另一个类。
};
//第二点是
void isincircle(Circle &c, Point &p)
{
int r2 = pow(c.get_r(), 2);
int d2 = pow(c.get_point().get_x() - p.get_x(), 2) + pow(c.get_point().get_y() - p.get_y(), 2);
if (d2 == r2)
{
cout << "点在园上" << endl;
}
else if (d2 > r2)
{
cout << "点在园外" << endl;
}
else
{
cout << "点在园内" << endl;
}
}
int main()
{
Circle c;
c.set_r(10);
Point c1;
c1.set_x(10);
c1.set_y(0);
c.set_point(c1);
Point p;
p.set_x(10);
p.set_y(10);
isincircle(c, p);
system("pause");
return 0;
}
补充:
当代码量大时候,将其放入头文件和源文件好操作:
各个部分代码如下:
头文件:此处类似声明函数
point.h:
#pragma once
#include<iostream>
using namespace std;
class Point
{
public:
void set_x(int x);
int get_x();
void set_y(int y);
int get_y();
private:
int p_x;
int p_y;
};
circle.h:
#pragma once//防止头文件重复
#include<iostream>
using namespace std;
#include"point.h"
class Circle
{
public:
void set_r(int r);
int get_r();
void set_point(Point x);
Point get_point();
private:
int m_r;
Point m_center;//第一点: 在类中一个类可以调用另一个类。
};
源文件://此处定义函数功能
point.cpp:
#include"point.h"
void Point::set_x(int x)
{
p_x = x;
}
int Point::get_x()
{
return p_x;
}
void Point::set_y(int y)
{
p_y = y;
}
int Point::get_y()
{
return p_y;
}
circle.cpp:
#include"circle.h"
void Circle::set_r(int r)//Circle::表示在Circle作用域下
{
m_r = r;
}
int Circle::get_r()
{
return m_r;
}
void Circle::set_point(Point x)
{
m_center = x;
}
Point Circle::get_point()
{
return m_center;
}
主程序进行调用:
#include<iostream>
using namespace std;
#include<string>
#include<Math.h>
#include <cstdlib>//解决system不明确问题。
#include"circle.h"
#include"point.h"
void isincircle(Circle &c, Point &p)
{
int r2 = pow(c.get_r(), 2);
int d2 = pow(c.get_point().get_x() - p.get_x(), 2) + pow(c.get_point().get_y() - p.get_y(), 2);
if (d2 == r2)
{
cout << "点在园上" << endl;
}
else if (d2 > r2)
{
cout << "点在园外" << endl;
}
else
{
cout << "点在园内" << endl;
}
}
int main()
{
Circle c;
c.set_r(10);
Point c1;
c1.set_x(10);
c1.set_y(0);
c.set_point(c1);
Point p;
p.set_x(10);
p.set_y(10);
isincircle(c, p);
system("pause");
return 0;
}
封装
Over…