栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++基础4---类和对象

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++基础4---类和对象

学习网站:C语言网.
C语言基础:C语言基础.
笔记及源码gitee地址:C++基础笔记及源码.
编译器:Red Panda Dev-C++.



1.类的定义
// 1.类:对象的抽象和概括;对象:类的具体和实例;
// 2.理解:类其实是包含函数的结构体;类可以包含基本变量,称为成员变量;可以包含函数,称为成员方法;
// 3.类中使用public控制成员访问权限的存取控制属性;
// 4.存取控制属性:public、private、protected;
// 5.存取控制属性说明:
// 5.1 public:可以被该类的任何对象访问,完全公开;
// 5.2 private:私有,被它声明的成员,仅仅能被该类里的成员访问,外界不能访问;
// 5.3 protected:除了类内自己的成员可以访问外,其子类也能访问;
#include 
using namespace std;

class Student {
	public:
		int num;		// 成员变量,称为属性; 
		char name[100];
		int score;
		int print(){	// 成员函数,称为方法; 
			cout << num << " " << name << " " << score;
			return 0;
		}
};		// 类定义后,具有分号;
// 类定义的令一种形式
#include 
using namespace std;

class Student {
	public:
		int studentID;		// 学生学号;
		char name[100];		// 学生姓名;
		int score;			// 学生成绩;
		int print();		// 类内声明print()函数; 
};

int Student::print(){		// 需要类名加::作用域限定符; 
	cout << studentID << " " << name << " " << score;
	return 0;
}
2.对象的建立和使用
// 1.对象的创建
// 类:包含函数的结构体,是一种自定义数据类型,用它定义的变量,即对象;通过.的方式访问类的成员;
// 如果想包含但又想控制的私有变量,通常将其声明为private类型,同时定义一个public类型的专门赋值的方法,由于内部成员可以访问private声明的变量,我们即可以在外部通过public方法来间接控制这些私有的成员,达到封装、保护的结果,这个public类型的方法,称为这个类的一个外部接口;
#include 
#include 
using namespace std;

class Student {
	public:
		int studentID;		// 学生学号;
		char name[100];		// 学生姓名;
		int score;			// 学生成绩;
		int print();		// 类内声明print()函数; 
};

int Student::print(){		// 需要类名加::作用域限定符;
	cout << "studentIDt" << "namett" << "score" << endl; 
	cout << studentID << "tt" << name << "tt" << score;
	return 0;
}

int main(){
	Student Willard;
	Willard.studentID = 829;
	strcpy(Willard.name, "Willard");
	Willard.score = 100;
	Willard.print();
	
	return 0;
}
// 2.对象的指针
// 对象指针:存储对象的地址;
// 定义方法:类名 *指针名;如:Student *p;
// 在传参时使用指针传递,不会进行对象间的副本赋值,减少内存开销,提高效率;
Student *p;
Student A;
p = &A;
p->print();		// 通过->访问对象中的成员;
// 3.对象的引用
// 对象引用:一个类对象起别名,本质上也把这个类对象的地址赋给这个引用类型,指向同一块内存空间;
Student A;
Student &Aq = A;	// 使用&定义一个该类类型的引用类型,并把A对象赋给Aq初始化;

// Tips:
// a.两者必须是同类型才可以引用;
// b.除非做函数的返回值或形参时,其余定义引用类型的同时就初始化;
// c.引用类型并非新建一个对象,不会调用构造函数;
// d.用引用类型时,本质上是存的地址,不会太多内存开销,有指针优势,做函数实参时,直接传入引用对象,不用加地址符,更直观、方便;
Student A;
Student &Aq = A;
Aq.print();
3.构造函数
// 1.构造函数:在类里,与类名同名,没有返回值,在定义一个类的对象,系统自动调用,进行专门的初始化对象;
#include 
#include 
using namespace std;

class Student {
	private:
		int studentID;
		char name[100];
		int score;
	public:
		Student(int n,char *str, int s);
		int print();
		int Set(int n, char *str, int s);
};

Student::Student(int n,char *str, int s){
	studentID = n;
	strcpy(name, str);
	score = s;
	cout << "Constructor" << endl;
}

int Student::print(){		// 需要类名加::作用域限定符;
	cout << "studentIDt" << "namett" << "score" << endl; 
	cout << studentID << "tt" << name << "tt" << score;
	return 0;
}

int Student::Set(int n, char *str, int s){
	studentID = n;
	strcpy(name, str);
	score = s;
	
	return 0;
}

int main(){
	Student Willard(100, (char*)"Willard", 100);
	Willard.print();
	return 0;
}
4.析构函数
// 1.在对象销毁时,自动调用一个函数,和类名同名,没有返回值,名字前有~,主要用做对象释放后的清理善后工作,即析构函数;
// 2.析构不能重载,但可以是虚函数,一个类只能有一个析构函数;
#include 
#include 
using namespace std;

class Student {
	private:
		int studentID;
		char name[100];
		int score;
	public:
		Student(int n,char *str, int s);
		~Student();
		int print();
		int Set(int n, char *str, int s);
};

Student::Student(int n,char *str, int s){
	studentID = n;
	strcpy(name, str);
	score = s;
	cout << "Constructor" << endl;
}

Student::~Student(){
	cout << "destructor" << endl;
}

int Student::print(){		// 需要类名加::作用域限定符;
	cout << "studentIDt" << "namett" << "score" << endl; 
	cout << studentID << "tt" << name << "tt" << score << endl;
	return 0;
}

int Student::Set(int n, char *str, int s){
	studentID = n;
	strcpy(name, str);
	score = s;
	
	return 0;
}

int main(){
	Student Willard(100, (char*)"Willard", 100);
	Willard.print();
	
	return 0;
}
5.拷贝构造函数
// 1.与类名同名,形参是本类对象的引用类型的函数,叫做拷贝构造函数,不主动定义的时,系统会自动生成,进行两个对象成员之间对象的简单赋值,用来初始化一个对象;
#include 
using namespace std;
#define PI 3.1415

class Circle {
	private:
		double R;
	public:
		Circle(double R);
		Circle(Circle &A);
		double area();
		double girth();
};

Circle::Circle(double R){
	cout << "Constructor" << endl;
	this->R = R;
}

Circle::Circle(Circle &A){
	cout << "Copy Constructor" << endl;
	this->R = A.R;
}

double Circle::area(){
	return PI*R*R;
}

double Circle::girth(){
	return 2*PI*R;
}

int main(){
	Circle A(5);
	Circle B(A);		// 通过对象A初始化B对象; 
	
	return 0;
}
6.浅拷贝和深拷贝
// 1.浅拷贝:把原有对象中成员依次拷贝给新对象中对应的成员;
// 2.浅拷贝属于简单初始化,但如果成员变量中有指针成员,初始化中需要动态开辟内存,则使用浅拷贝会出现极大安全隐患;
// 3.深拷贝:当类中有指针类型时,需要定义一个特定的拷贝构造函数,不仅可以进行数据的拷贝,同时可以为成员分配内存空间,实现真正的拷贝;

// Tips:
// 需要自行查询其他资料理解深拷贝;
#include 
#include 
using namespace std;
#define PI 3.1415

class Circle {
	private:
		double R;
		char *str;
	public:
		Circle(double R,char *str);
		Circle(Circle &A);
		~Circle();
		double area();
		double girth();
};

Circle::~Circle(){
	delete []str;
	cout << "Call Destructor" << endl;
}

Circle::Circle(Circle &A){
	cout << "Copy Constructor" << endl;
	this->R = A.R;
	this->str = new char[strlen(A.str) + 1];
	strcpy(this->str,A.str);
}

Circle::Circle(double R,char *str){
	cout << "Constructor" << endl;
	this->R = R;
	this->str = new char[strlen(str) + 1];
	strcpy(this->str,str);
}

double Circle::area(){
	return PI*R*R;
}

double Circle::girth(){
	return 2*PI*R;
}

int main(){
	Circle A(5,(char *)"NO.1 Old class");
	Circle B(A);
	return 0;
}
7.this指针
// 1.this指针:只要定义一个类,系统会预定义this指针,指向当前对象;
// 时钟类的一个成员函数,用来设置时间传值
int Clock::SetTime(int h,int m,int s){
    H = h;
    M = m;
    S = s;
}

// this用法
int Clock::SetTime(int h,int m,int s){
    this->H = h;
    this->M = m;
    this->S = s;
}

// this另一种用法
int Clock::SetTime(int h,int m,int s){
    (*this).H = h;
    (*this).M = m;
    (*this).S = s;
}
8.友元函数
// 1.在类外想访问私有成员,有时候会出现既访问不到又不能声明为public类型的处境;
// 2.把外部的函数声明为友元类型,赋予它可以访问类内私有成员的权利;
// 3.友元对象,可以是全局的一般函数,可以是其他类里的成员函数,称为友元函数;友元可以是一个类,称为友元类;

// 友元函数
#include 
#include 
using namespace std;

// 求两点的距离; 
class Point {
	private:
		double x;
		double y;
	public:
		Point(double a,double b){
			x = a;
			y = b;
		}
		int GetPoint(){
			cout << "(" << x << "," << y << ")";
			return 0;
		}
		friend double Distance(Point &a,Point &b);
};

// 求两点间的距离;
double Distance(Point &a,Point &b){
	double xx;
	double yy;
	xx = a.x - b.x;
	yy = a.y - b.y;
	
	return sqrt(xx * xx + yy * yy);
}

int main(){
	Point A(2.0,3.0);
	Point B(1.0,2.0);
	double dis;
	dis = Distance(A,B);
	cout << dis << endl;
	
	return 0;
} 
// 友元类
#include 
#include 
using namespace std;

class Point {
	private:
		double x;
		double y;
	public:
		Point(double a,double b){
			x = a;
			y = b;
		}
		int GetPoint(){
			cout << "(" << x << "," << y << ")";
			return 0;
		}
		int distancetoLine(){
			return 0;
		}
	friend class Tool;
};

class Tool {
	public:
		double GetX(Point &A){
			cout << A.x << endl;
			return A.x;
		}
		double GetY(Point &A){
			cout << A.y << endl;
			return A.y;
		}
		double dis(Point &A){
			cout << sqrt(A.x * A.x + A.y * A.y) << endl;
			return sqrt(A.x * A.x + A.y * A.y);
		}
};

int main(){
	Point A(2.0,3.0);
	Tool T;
	T.GetX(A);
	T.GetY(A);
	T.dis(A);
	
	return 0;
}
9.常数据使用及初始化
// 1.const:被常修饰的变量,是不可修改的;
// 2.const可以修饰一般变量为常变量,修饰类的数据成员和成员函数,称为类的常数据成员和常成员函数;

// 1.常数据成员

#include 
using namespace std;

class Clock {
	private:
		const int h;	// 修饰h为常类型成员;
		const int m;
		int const s;
		static const int x;		// 需要在类外进行初始化;
	public:
		Clock(int a,int b,int c):h(a),m(b),s(c){
			cout << "Constructor! Called" << endl;
		} 
		
		int ShowTime(){
			cout << h << ":" << m << ":" << s << endl;
			return 0;
		}
		
		int GetX(){
			cout << x << endl;
			return 0;
		}
};

const int Clock::x = 99;
int main(){
	Clock A(12,10,30);
	A.ShowTime();
	A.GetX();
	
	return 0;
}
// 2.常对象
// 常对象在整个声明周期中不可以被更改,在定义的时候由构造函数进行初始化;

// 常对象不可以访问类中的非常成员函数,只能访问常成员函数;
#include 
using namespace std;

class Clock {
	private:
		const int h;
		const int m;
		int const s;
		int x;
	public:
		Clock(int a,int b,int c):h(a),m(b),s(c){
			x = 99;
			cout << "Constructor!Called" << endl;
		}
		int ShowTime(){
			cout << h << ":" << m << ":" << s << endl;
			return 0;
		}
		int GetX() const{
			cout << x << endl;
			return 0;
		}
};

int main(){
	const Clock A(12,10,30);
	const Clock B(14,20,50);
	
	// A = B;			// 会报错; 
	// A.ShowTime();
	A.GetX();
	
	return 0;
}
// 3.常成员函数
// 一个类中的成员函数被const修饰后,称为常成员函数;

#include 
using namespace std;

class Clock {
	private:
		const int h;
		const int m;
		int const s;
		int x;
	public:
		Clock(int a,int b,int c):h(a),m(b),s(c){
			x = 99;
			cout << "Constructor!Called" << endl;
		}
		int ShowTime(){
			cout << h << ":" << m << ":" << s << endl;
			return 0;
		}
		int GetX() const{
			// x = 99;
			cout << x << endl;
			return 0;
		}
};

int main(){
	const Clock A(12,10,30);
	A.GetX();
	
	return 0;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/870055.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号