c++封装 构造函数 析构函数 拷贝函数

本文详细介绍了C++中的类和对象概念,包括封装的含义,以及构造函数、析构函数的作用。通过实例演示了如何定义和使用属性、行为,以及构造函数的不同类型和调用方式,如无参构造、有参构造和拷贝构造。

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


/*
类和对象 —封装 —属性和行为作为整体
c++面向对象的三大特征 :封装 继承 多态
c++认为万事万物都皆为对象,对象上有其属性和行为
封装的意义:
1.将属性和行为作为一个整体,表现生活中的事物
2.将属性和行为加以限权控制
*/
/*
封装意义一:
在设计类的时候,属性和行为写在一起,表现事物
语法:class 类名 { 访问权限 :属性/行为}
示例1:设计一个圆类 求周长
圆求周长的公式2PIr
class 代表设计一个类 类后面紧跟着类的名称
类中的属性和行为 统一称为 成员
属性 成员属性 成员变量
行为 成员函数 成员方法
*/
/*
#include<iostream>
using namespace std;
const double PI=3.14;
class Circle
{

    public://访问权限 (//公告权限)
    
        int r;    //属性 (变量) 半径
        double cal()  //行为(函数) 获取圆的周长
        {
            return 2*PI*r;
        }
};
int main()
{
    //通过圆类 创建具体的圆(对象)
    //实例化(通过一个类 创建一个对象的过程)
    Circle c1;
    c1.r=10;    //给圆对象 赋值
    cout << "圆的周长为:" << c1.cal() << endl;
    return 0;
}
*/

/*
//示例2:设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,可以显示学生的姓名和学号
#include<iostream>
#include<string>
using namespace std;
class Student 
{
    public:
           string name;
        int id;
        void showStudent()
        {
            cout << "姓名是" << name <<endl;
            cout << "学号是" << id << endl;
        }
        
};
int main()
{
    Student s1;
    s1.name="张三";
    s1.id=1;
    s1.showStudent();
    
    Student s2;
    s2.name="李四";
    s2.id=2;
    s2.showStudent();
    return 0;
}
*/

/*
#include<iostream>
#include<string>
using namespace std;
class Student 
{
    public:
           string name;
        int id;
        void showStudent()
        {
            cout << "姓名是" << name <<endl;
            cout << "学号是" << id << endl;
        }
        //给姓名赋值
        void setName(string name1)
        {
            name=name1;
        }
        //给学号赋值
        void setId (int id1)
        {
            id=id1;
        }
        
};
int main()
{
    Student s1;
    s1.setName("张三");
    s1.setId(1);
    s1.showStudent();
    return 0;
}
*/


/*
对象的初始化和清理
构造函数(初始化)和析构函数(清理)
如果我们不提供构造函数和析构函数,编译器会提高,但是编译器提供的构造函数和析构函数是空实现
构造函数:主要作用在于创建对象时为对象的成员属性赋值,构造函数由编译器自动调用 无须手动调用
析构函数:主要作用在于对象销毁前系统自动调用,执行一些清理工作
构造函数语法:类名(){}
1.没有返回值 不用写 void
2.函数名 与类名相同
3.可以有参数 因此可以发生重载
4.程序在调用对象的时候会自动调用构造函数 而且只会调用一次
析构函数:~类名(){}
1.没有返回值 不用写 void
2.函数名与类名相同 在名称前加上~
3.不可以有参数 因此不可以发生重载
4.程序在对象销毁前会自动调用析构函数 而且只会调用一次
*/

/*
#include<iostream>
using namespace std;
class Person
{
public:
    Person() //构造函数
    {
        cout << "构造函数的调用"<< endl;
    }
    
    ~Person()
    {
        cout << "析构函数的调用" << endl;
    }
};

void test01()
{
    Person p;//在栈上的数据 test01执行完毕后就会释放这个对象
}
int main()
{
    //test01();
    Person p;//只有构造 没有析构 因为并没有被释放
    system("pause");
    return 0;
}
*/


/*
构造函数的分类以及调用
两种分类方式:
按参数:有参构造 无参构造(默认构造)
按类型:普通构造 拷贝构造
三种调用方式:
1.括号法
2.显示法
3.隐式转换法
*/
/*
#include<iostream>
using namespace std;
class Person
{
public:
    int age;
    Person()//无参构造    
    {
        cout << "Person 无参构造函数调用 " << endl;
    }
    Person(int a)//有参构造    
    {
        age=a;
        cout << "Person 有参构造函数调用 " << endl;
    }
    
    ~Person()//析构函数    
    {
        cout << "Person 析构函数调用 " << endl;
    }
    
    Person(const Person &p)//拷贝函数
    {
        age=p.age;//将传入的人身上的所有属性 ,拷贝到我身上
        cout << "Person 拷贝函数调用 " << endl;
    }
};
//调用
void test01()
{
    //1.括号法
//    Person p1;//默认构造函数调用
//    Person p2(10);//有参构造函数
//    Person p3(p2);//拷贝构造函数
//    cout << "p2年龄" << p2.age<< endl;
//    cout << "p3年龄" << p3.age<< endl;
    //注意事项1:
    //调用默认构造函数时候不要加()
    //因为这个代码 编译器会认为是一个函数的声明,不会认为在创建对象//Person p1();
    
    //2.显示法
//    Person p4;
//    Person p5=Person (10);//有参构造
//    Person p6=Person(p5);//拷贝构造
//    Person(10);//匿名对象 特点:当前行执行结束后,系统会立即回收掉匿名对象
    //注意事项2:不要利用拷贝构造函数 初始化匿名对象
    
    //3.隐式转换法
//    Person p7 =10;//相当于Person p7 =Person (10) 有参
//    Person p8 =p7;//拷贝构造
}
int main()
{
    test01();
    return 0;
}
*/

/*
拷贝构造函数调用时机
1.使用一个已经创基完毕的对象来初始化一个新对象
2.值传递的方式给函数参数传值
3.以值方式返回局部对象
*/
#include<iostream>
using namespace std;
class Person
{
public:
    int mage;
    Person()
    {
        cout << "默认构造函数调用"<< endl;
    }
        Person(int age)
        {
            cout << "有参构造函数调用"<< endl;
             mage=age;
        }
            Person(const Person & p)
            {
                cout << "拷贝函数调用"<< endl;
               mage=p.mage;
            }
        ~Person()
        {
            cout << "析构函数调用"<< endl;
        }
};
//1.使用一个已经创基完毕的对象来初始化一个新对象
void test01()
{
    Person p1(20);
    Person p2(p1);
    cout << "p2的年龄" << p2.mage<< endl;
}
//2.值传递的方式给函数参数传值
void dowork( Person p)
{
    
}
void test02()
{
    Person p;
    dowork(p);//拷贝
}

//3.以值方式返回局部对象
Person dowork2()
{
    Person p1;
    return p1;
}
void test03()
{
    Person p=dowork2();
}
int main()
{
    //test01();
    //test02();
    test03();
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值