C++学习-2023/12/14-C++类和对象

2023-12-14 13:03:36

类和对象

类是一系列具有共同属性和行为的事物的抽象
对象是类的具体化(实例化)
属性:数据成员(int,float)描述,事务的共有特征
行为:成员函数(类中函数),一系列共有事务操作

类的特点:封装继承多态
//类中默认为private属性,结构体中默认是public属性

没有写在范围内默认为private属性

一般写项目都是保护属性

一般情况类外访问类中数据必须通过对象来访问

静态数据成员和静态成员也受权限限定,可以不需要对象也可以调用

class 类名
{
public:
protected:
private:
};

class MyClass
{
public:
	MyClass();
	~MyClass();

private:

};

代码块

#include <iostream>
#include <string>
class Book
{
public:
	Book();
	~Book();
	int num = 0;
	void print();
protected:
	std::string name = "笔记本";  //数据直接初始化
private:
	int money = static_cast<int>(2.22);
};
void Book::print()
{
	std::cout << name << "\t" << num << "\t" << money << "\n";
}

Book::Book()
{
}

Book::~Book()
{
}


int main()
{
	Book book;
	book.num = 10086;
	book.print();
    std::cout << "Hello World!\n";
}

类中数据访问

啥是接口

C++当中一般是指public属性下的方法

#include <iostream>
#include <string>
class Book
{
public:
	Book();
	~Book();
	void Print();
	void SetData(const std::string xName, const int xNum, const int money);
	//可能看到的-----跟C#有一丢丢像
	std::string& setString(const std::string xName);
	std::string& getString();

protected:
	int num = 0;
	std::string name = "笔记本";  //数据直接初始化
private:
	int money = static_cast<int>(2.22);
};
void Book::Print()
{
	std::cout << name << "\t" << num << "\t" << money << "\n";
}

void Book::SetData(const std::string xName, const int xNum, const int xmoney)
{
	name = xName;
	num = xNum;
	money = xmoney;
}

std::string& Book::setString(const std::string xName)
{
	name = xName;
	return name;
}
std::string& Book::getString()
{
	return  name;
}

Book::Book()
{
}

Book::~Book()
{
}


int main()
{
	Book book;
	book.Print();
	book.SetData("横滨七海", 110, 120);
	book.Print();
	book.setString("缅怀");
	book.getString();
	book.Print();

	book.getString() = "6";
	book.Print();
    std::cout << "Hello World!\n";
}

类中的各种形式

#include <iostream>
#include <string>
class Book
{
public:
	Book();
	~Book();
	void Print();
	void PrintBook(Book& object);
	void SetData(const std::string xName, const int xNum, const int money);
	//可能看到的-----跟C#有一丢丢像
	std::string& setString(const std::string xName);
	std::string& getString();

protected:
	int num = 0;
	std::string name = "笔记本";  //数据直接初始化
private:
	int money = static_cast<int>(2.22);
};
void Book::Print()
{
	std::cout << name << "\t" << num << "\t" << money << "\n";
}

void Book::PrintBook(Book& object)
{
	object.Print();
}

//Book ReturnBook()
//{
//	Book* p = new Book;
//	p->setString("七伤拳");
//	return *p;
//}

Book* ReturnBook()
{
	Book* p = new Book;
	p->setString("七伤拳");
	return p;
}


void Book::SetData(const std::string xName, const int xNum, const int xmoney)
{
	name = xName;
	num = xNum;
	money = xmoney;
}

std::string& Book::setString(const std::string xName)
{
	name = xName;
	return name;
}
std::string& Book::getString()
{
	return  name;
}

Book::Book()
{
}

Book::~Book()
{
}


int main()
{
	//1.普通对象
	Book book;
	book.PrintBook(book);
	ReturnBook()->Print();
	Book* pp = ReturnBook();
	delete ReturnBook();//这样可以?
	//有个问题,函数创建的堆区返回后如何清理回收
	ReturnBook()->Print();

	//2.对象数组
	Book bookArray[3];
	for (int i = 0; i < 3; ++i) {
		bookArray[i].Print();
		(bookArray + i)->Print(); //等校
	}
	//对象指针
	Book* pBook = &book;
	pBook->SetData("哇咔咔", 12345, 67890);
	pBook->Print();

	//4.new一个对象
	pBook = new Book;
	pBook->Print();
	delete pBook;

	pBook = new Book[4];
	for (int i = 0; i < 3; ++i) {
		std::string str = "爱莉";
		std::string str2 = str + std::to_string(i);
		(pBook + i)->getString() = str2;
		(pBook + i)->Print(); //等校
	}
	delete[] pBook;
	pBook = nullptr;



    std::cout << "Hello World!\n";
}

文章来源:https://blog.csdn.net/weixin_47454452/article/details/134987735
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。