C++ 类和对象(构造和析构)

本文深入探讨了C++中的类与对象概念,包括类的定义、封装、构造与析构函数等核心内容,并通过矩阵类实例展示了如何使用类进行矩阵乘法运算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

类:自定义数据类型
对象:用类定义的变量

1. 类的定义和封装
class 类名 // 类声明( Declaration)部分
{
public :
公有成员
protected :
保护成员
private :
私有成员
} ;
void function()
{
}//各函数成员的完整定义 // 类实现( Implementation)部分
语法说明:
1. class是定义类时的关键字;
2. 类名需符合标识符的命名规则;
3. 类成员有2种,分别是数据成员和函数成员。某些特殊的类可能只包含1种成员,比如只包含数据成员,或只
包含函数成员;
4. 访问权限有3种,分别是public(公有权限)、 protected(保护权限)和private(私有权限),
public类型可以在外部调用,成员和类的默认访问修饰符是 private。
5. 类定义代码分为2个部分。 类声明部分在大括号中声明数据成员、函数成员,并指定各成员的访问权限。声明
数据成员的语法形式类似于定义变量语句,但声明时不能初始化。声明函数成员只是声明其原型,完整的函
数定义代码放在大括号外面的类实现部分,定义时需在函数名前加“类名::”限定,指明该函数属于哪个类;
 6.函数成员可以访问本类中任意位置的数据成员,或调用本类中任意位置的函数成员。类成员之间互相访问不
需要“先定义,后访问”,或“先声明,后访问”,也不受权限约束。在类定义代码(包含声明和实现2部分)
范围内,任何类成员都可以被本类的其它成员访问,类成员具有类作用域( Class scope);
 7.不同类作用域之间的标识符可以重名,即不同类的成员之间可以重名。
     8.在类定义中定义的成员函数把函数声明为内联的,即便没有使用 inline 标识符。

例如定义一个矩形
class Rectangle             //class关键字,Rectangle类名,声明(成员)
{
public:                     
    double a,b;
    double RArea();
    double Rlen();
};
//实现 函数成员的具体定义
double Rectangle::RArea()	//成员函数可以定义在类定义内部,或者单独使用范围解析运算符 :: 来定义。
{
    return(a*b);
}
double Rectangle::Rlen()
{
    return(2*a+2*b);
}

//调用成员函数是在对象上使用点运算符(.),这样它就能操作与该对象相关的数据.

Rectangle rec1;		//定义矩阵对象rec1,也可以定义对象指针
rec1.RArea();

2. 构造函数和析构函数
创建对象的过程称为对象的构造,销毁对象的过程称为对象的析构
构造函数
– 构造函数名必须与类名相同。
– 构造函数由计算机自动调用,程序员不能直接调用。
– 构造函数通过形参传递初始值(可指定默认形参值),实现对新
建对象数据成员的初始化。
– 构造函数可以重载,即定义多个同名的构造函数,这样可提供多
种形式的对象构造方法。
– 构造函数可以定义成内联函数。
– 构造函数没有返回值,定义时不能写函数类型,写void也不行。
– 构造函数通常是类外调用,其访问权限应设为public或protected,
不能设为private。
– 一个类如果没有定义构造函数,编译器在编译时将自动添加一个
空的构造函数,称为默认构造函数,其形式为:
类名( ) { }
class Rectangle             //class关键字,Rectangle类名,声明(成员)
{
private:             
    double a,b;
public:
    Rectangle()			//构造函数
    {
           a = 0;
           b = 0;
    }
    double RArea();
    double Rlen();
};
析构函数
– 析构函数名必须为“ ~类名”。
– 析构函数由计算机自动调用,程序员不能直接调用。
– 析构函数没有形参。
– 析构函数没有返回值,定义时不能写函数类型,写void也不
行。
– 一个类只能有一个析构函数。
– 析构函数通常是类外调用,其访问权限应设为public或
protected,不能设为private。
– 一个类如果没有定义析构函数,编译器在编译时将自动添加
一个空的析构函数,称为默认析构函数,其形式为:
~类名( ) { }

3.作业:矩阵乘法
用户输入矩阵1行数、列数,然后提示用户输入各元素;
用户输入矩阵2行数、列数,然后提示用户输入各元素;
输入后计算相乘结果矩阵。
要求:
1.定义矩阵类
2.构造和析构
3.使用二维指针及动态分配,完成作业。

matrix.h文件:
#pragma once
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
using namespace std;
class matrix
{
public:
	int row;
	int column;
	int **ptr;
	matrix()
	{
		cout << "please input row and column:" << endl;
		cin >> row >> column;
		cout << row << "x" << column << endl;
		ptr = new int*[5];			//int*加括号是错的,不能写成ptr = new (int*)[5] 
		for (int i = 0;i < column;i++)
		{
			ptr[i] = new int[column];
		}
	}
	~matrix()
	{
		for (int i = 0;i < column;i++)
		{
			delete[]ptr[i];
		}
		delete[]ptr;
	}
	void input_matrix(void);
	void matrix_display(void);
};

void matrix::input_matrix(void)
{
	cout << "please input the matrix:" << endl;
	for (int i = 0;i < row;i++)
	{
		for (int j = 0;j < column;j++)
		{
			cin >> ptr[i][j];
		}
	}
}
void matrix::matrix_display(void)
{
	for (int i = 0;i < row;i++)
	{
		for (int j = 0;j < column;j++)
		{
			cout << ptr[i][j]<<" ";
		}
		cout << endl;
	}
}
void matrix_mul(matrix *matrix1, matrix *matrix2, matrix *matrix3)
{
	if (matrix1->column != matrix2->row)
	{
		cout << "error!" << endl;
		exit(0);
	}
	for (int i = 0;i < matrix1->row;i++)
	{
		for (int j = 0;j < matrix2->column;j++)
		{
			matrix3->ptr[i][j] = 0;
			for (int k = 0;k < matrix1->column;k++)
			{
				matrix3->ptr[i][j] += matrix1->ptr[i][k] * matrix2->ptr[k][j];
			}
		}
	}
}

void matrix_and(matrix *matrix1, matrix *matrix2, matrix *matrix3)
{
	if (matrix1->row != matrix2->row || matrix1->column != matrix2->column)
	{
		cout << "error!" << endl;
		exit(0);
	}
	for (int i = 0;i < matrix1->row;i++)
	{
		for (int j = 0;j < matrix2->column;j++)
		{
			matrix3->ptr[i][j] = matrix1->ptr[i][j] + matrix2->ptr[i][j];
		}
	}
}

#endif // !MATRIX_H
matrix.cpp文件:
#include <iostream>
#include "matrix.h"
#include <malloc.h>
int main()
{
	char ch;
	int **p;
	matrix *matrix1 = new matrix();			//为类指针分配空间
	matrix *matrix2 = new matrix();
	matrix *matrix3 = new matrix();
	
	matrix1->input_matrix();
	matrix2->input_matrix();
	matrix1->matrix_display();
	matrix2->matrix_display();
	matrix3->row = matrix1->row;
	matrix3->column = matrix2->column;
	cout << "please output the operation:" << endl;
	cin >> ch;
	if (ch == '+')
	{
		matrix_and(matrix1, matrix2, matrix3);

		getchar();
	}
	else
	{ 
		if (ch == '*')
		{
			matrix_mul(matrix1, matrix2, matrix3);
		}
		else
		{
			cout << "error2!" << endl;
			exit(0);
		}
	}
	cout << "output is:" << endl;
	matrix3->matrix_display();
	getchar();
	getchar();
	return 0;
}




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值