回忆C++知识点(1)

重载函数

#include <iostream>
using namespace std;
int Add(int x, int y) {//定义第一个重载函数
	cout << "int add" << endl;
	return x + y;
}
double Add(double x, double y) { //定义第二个重载函数
	cout << "double add" << endl;
	return x + y;
}
void main()
{
	int var = Add(5, 2);
	float fvar = Add(10.5, 11.4);
	
}

成员函数重载

#include "pch.h"
#include<string>
#include<iostream>
using namespace std;
class Book
{
public:
	Book(const char *sname,const char *sauthor, double sprice)
	{
		strcpy(name, sname);
		strcpy(author, sauthor);
		price = sprice;
	}
	char *GetName() { return name; }
	char *GetAuthor() { return author; }
	double GetPrice() { return price; }
	void Display()
	{
		cout << "书名:" << name << "作者:" << author << "价格:" << price << endl;
	}
private:
	char name[50];
	char author[20];
	double price;
};


class OverLoad
{
public:
	bool IsEqual(int a, int b);     //重载
	bool IsEqual(Book & a, Book & b);
};
bool OverLoad::IsEqual(int a, int b)
{
	if (a = b)return true;
	else return false;
}
bool OverLoad::IsEqual(Book & a, Book & b)
{
	if (strcmp(a.GetName(), b.GetName()) == 0 && strcmp(a.GetAuthor(), b.GetAuthor()))return true;
	else return false;
}
int main()
{
	Book b1("C++入门", "小林", 59.8);
	Book b2("python入门", "大林", 78.9);
	int a = 5;
	int b = 5;
	OverLoad o1;
	if (o1.IsEqual(a, b))cout << a << " = " << b << endl;
	if (o1.IsEqual(b1, b2))cout << "两本书相同" << endl;
	else {
		b1.Display();
		b2.Display();

	}
	return 0;
}

隐藏的this指针

对于类的非静态成员,每个对象都有自己的数据成员,不过成员函数却是每个对象共享的。那么调用共享的成员函数是如何找到自己的数据成员的呢?

#include "pch.h"
#include<iostream>
using namespace std;

class Rectangle
{
public:
	inline void SetValue(double width, double height);
	inline double GetArea();
	inline void GetThis();
private:
	double width;
	double height;

};
void Rectangle::SetValue(double width, double height)
{
	this->width = width;//数据成员与形参名相同时,使用this指针指定数据成员
	this->height = height;
}
double Rectangle::GetArea()
{
	return width * height;
}
void Rectangle::GetThis()
{
	cout << "this指针保存的地址值" << this << endl;
}
int main()
{
	Rectangle r1, r2;//创建两个对象
	r1.SetValue(5, 6);//对象调用成员函数,设置该对象中的数据成员的值
	cout << "矩形1的地址" << &r1 << endl;
	r1.GetThis();
	cout << "面积:" << r1.GetArea() << endl;
	cout << endl;
	r2.SetValue(4, 6.5);
	cout << "矩形2的地址" << &r2 << endl;
	r2.GetThis();
	cout << "面积:" << r2.GetArea() << endl;
	return 0;

}

调用构造函数和析构函数的调用顺序

一般情况下,调用析构函数的次序正好与调用构造函数的次序相反,也就是最先被调用的构造函数,其对应的析构函数最后调用,而最后被调用的构造函数,其对应的析构函数最先被调用。

#include "pch.h"
#include<iostream>
#include<string>
using namespace std;
void OutPut();//普通函数

class Student
{
public:
  Student() :age(0)
  {
	  strcpy(name, "无名");
	  cout << name << ":默认构造函数被执行" << endl;
   }
   Student(const char *sname,int sage)
   {
	  strcpy(name,sname);
		   age = sage;
	  cout << name << ":带参数构造函数被执行" << endl;
   }
   ~Student()
   {
	  cout << name << ":析构函数被执行" << endl;
   }
   void Display();
private:
   char name[20];
   int age;
}student("Jack", 23);//全局对象


void Student::Display()
{
	cout << "姓名" << name << "   " << "年龄:" << age << endl;
}

void OutPut()
{
	static Student stu;//静态局部对象
}
int main()
{
	Student s1("Lucy", 22);//局部对象
	Student s2("Lily", 22);//局部对象
	OutPut();
	cout << "exit mian..." << endl;
	return 0;
}

运行结果:
Jack:带参数构造函数被执行
Lucy:带参数构造函数被执行
Lily:带参数构造函数被执行
无名:默认构造函数被执行
exit mian...
Lily:析构函数被执行
Lucy:析构函数被执行
无名:析构函数被执行
Jack:析构函数被执行



















 

原文地址:https://www.cnblogs.com/alec7015/p/12445814.html