c++(六) 类和对象

1.数组对象的创建

和普通数组的使用类似

  Box boxes[3] = {
            Box{1, 2, 3},
            Box{4, 5, 6},
            Box{7, 8, 9}
    };
    cout << boxes[1].length;

2.头文件的创建(coordin.h)

#ifndef DEMO624_COORDIN_H //防止重复声明
#define DEMO624_COORDIN_H

struct polar {
    double distance;
    double angel;
};
struct rect {
    double x;
    double y;
};

polar rect_to_polar(rect xypos);
void show_polar(polar dapos);

#endif //DEMO624_COORDIN_H

3.引用头文件(file1.cpp,file2.cpp)

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

int main() {
    rect rplace;
    polar pplace;

    cout << "Enter the x and y values: ";
    while (cin >> rplace.x >> rplace.y) {
        pplace = rect_to_polar(rplace);
        show_polar(pplace);
        cout << "Next two numbers (q to quit):";
    }
    cout << "Bye! \n";
    return 0;
}
#include <iostream>
#include <cmath>
#include "coordin.h"

polar rect_to_polar(rect xypos){
    using namespace std;
    polar answer;

    answer.distance = sqrt(xypos.x * xypos.x + xypos.y * xypos.y);
    answer.angel = atan2(xypos.y, xypos.x);
    return answer;
}

void show_polar(polar dapos){
    using namespace std;
    const double Rad_to_deg = 57.29577951;

    cout <<"distance = " << dapos.distance;
    cout << ", angle = " << dapos.angel * Rad_to_deg;
    cout << " degress\n";
}

4.配置文件(CMakeLists.txt)

cmake_minimum_required(VERSION 3.28)
project(untitled1)

set(CMAKE_CXX_STANDARD 17)

add_executable(untitled1 main.cpp
        coordin.h
        file2.cpp
        file1.cpp)

5.运算符重载

我们常见的+、-、*、/除了字面功能,还能用于其他功能,如*可以声明指针等。是另一种形式的多态性。

比如以下代码,重载了运算符加号+ ,实现了时间累加功能。

 //函数声明
 Time operator+(const Time &t) const;

 //函数实现
 Time Time::operator+(const Time &t) const {
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;
    //两小时相加(1小时20分,2小时50分)
    //小时数相加,分钟时相加满60进1
    return sum;
}

 int main(){
   //函数两种调用方式
   time1.operator+(time2);
   time3 + time4;
   return 0;
}
可以重载的运算符:
+ - * /
%
^
&
~= !
=
<
>
+=
-=
*=
/=
%=
^=
&=
=
<<
>>
>>=
<<=
==
!=
<=
>=
&&
ǁ ++ −− 
−>*
−>
() [] new delete new [] delete []

6.友元函数

6.1 简介

在 C++中 , " 友元函数 " 是 与 类 相关联的函数 。虽然不是类的成员函数 , 但是可以访问类的 private 私有成员 和 protected 保护成员 ;

友元函数可以是 全局函数、本类的成员函数、其他类的成员函数

友元函数是 类的 朋友 ,

在友元函数中 , 可以修改类对象中的 私有属性 和 保护属性 ;

友元函数破坏了类的封装性。 

6.2 特点

使用方法:friend void changeAge(Student* s, int age);

其中friend关键字是必须的,而且参数至少有一个是类对象的指针;

它不受类的访问控制限制 , 可以在任何地方定义 , 如 : 在 private: , protected: , public:。

#include <iostream>

class Student {
private:
    int age;
    int height;
    //声明了友元函数
    friend void changeAge(Student *s, int age);
public:
    Student(int age = 1, int height = 1) {
        this->age = age;
        this->height = height;
        std::cout << "构造函数" << std::endl;
    }
    ~Student() {
        std::cout << "析构函数" << std::endl;
    }
    void print() const {
        std::cout << "age = " << age << ", height = " << height << std::endl;
    }
};

//实现了友元函数
void changeAge(Student *s, int age) {
    s->age = age;
}

int main() {
    using std::cout;
    using std::endl;
    Student s(18, 100);
    s.print();
    changeAge(&s, 88);//调用友元函数,成功修改了类的私有属性age
    s.print();
    return 0;
}

7.转换函数

对象的强制类型转换

operator typeName( )
如operator int(),operator double();
explicit operator int () const{
        return age;
}

int main() {
 Student s(18, 100);
 cout << (int) s;
 // 输出18
}

8.explicit作用

任何接 受唯一一个参数的构造函数都可被用作转换函数。
在该构造函数的声明前加上了关键字 explicit ,则该构造函数将只能用于显式转换。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值