首页 > 编程语言 > 详细

(c++)类与对象

时间:2020-12-11 14:18:47      阅读:25      评论:0      收藏:0      [点我收藏+]

c++类与对象

1.类声明

#include <iostream>
using namespace std;
class studend
{
    private:
    int age;
    public:
    void chage(int age);
    void prtage();
};

void studend::chage(int a){
    age = a;
}

void studend::prtage(){
    cout<<age<<‘\n‘;
}



int main(int argc, const char** argv) {
    studend first;
    first.chage(10);
    first.prtage();
    return 0;
}

result:
10

2.类中static变量与函数

功能:实现类的属性与方法

表现:

  • 只存储一次
  • 属性必须初始化

3.类的构造与析构函数

#include <iostream>
using namespace std;    
class Studend
{
    public:
    Studend();
    ~Studend();
};
Studend::~Studend(){
    cout<<"i am over"<<‘\n‘;
}
Studend::Studend()
{
    cout<<"i am coming!"<<‘\n‘;
}
int main(int argc, const char **argv)
{
    Studend first;
    return 0;
}

assemble:
callq  0x55555555521e <Studend::Studend()>;
callq  0x5555555551ea <Studend::~Studend()>

    result:
i am coming!
    i am over

复制构造函数

#include <iostream>
using namespace std;
class Studend
{
    private:
    int age;

    public:
    Studend(){
        cout<<"is start"<<endl;
    }

    Studend(Studend &dcopy){ //复制构造函数
        age = dcopy.age;
    }

    void setAge(int age1){
        age = age1;
    }

    void show(){
        cout<<age<<endl;
    }

    ~Studend(){
        cout<<"is over"<<endl;
    }
};


int main(int argc, const char **argv)
{
    Studend first;
    first.setAge(10);
    first.show();
    Studend second(first); 
    second.show();
    return 0;
}

result:
is start
    10
    10
    is over
    is over

4.友元函数与类

适用场合:

类外调用类的私有属性与方法

#include <string.h>
#include <iostream>
using namespace std;
class Studend
{
    private:
    int id;
    char name[10];
    public:
    Studend(){}
    Studend(Studend &dcopy)
    {
        id = dcopy.id;
        strcpy(name, dcopy.name);
    }
    Studend(int Newid, char *NewName)
    {
        id = Newid;
        strcpy(name, NewName);
    }
    /*
            函数功能:比较学生的名字是否相等
            函数入口:学生1 学生2
            函数出口:无
            注意事项:无
    */
    friend void EqName(const Studend &eqStudent0,
                       const Studend &eqStudent1)
    {
        if(!strcmp(eqStudent1.name,eqStudent0.name)){
            std::cout << "they are eq" << std::endl;
        }
        else
        {
            std::cout << "they are not eq" << std::endl;
        }

    }
}
    
int main(int argc, const char **argv)
{
    char name[10] = "haoge";
    Studend first(10, name);
    Studend tow;
    tow = first;
    EqName(tow,first); //友元函数调用
    return 0;
}

result:

they are eq

5.this指向非静态成员(对象)

适用场合:对象的方法

#include <string.h>
#include <iostream>
using namespace std;
class Studend
{
    private:
    int id;
    char name[10];
    public:
    Studend(){}
    Studend(Studend &dcopy)
    {
        id = dcopy.id;
        strcpy(name, dcopy.name);
    }
    Studend(int Newid, char *NewName)
    {
        id = Newid;
        strcpy(name, NewName);
    }
    /*
            函数功能:比较学生的名字是否相等
            函数入口:学生
            函数出口:无
            注意事项:属于对象的方法
    */
    void Eq(const Studend &studend){
        if(!strcmp(this->name,studend.name)){
        std::cout << "they are eq" << std::endl;
        }
        else
        {
            std::cout << "they are not eq" << std::endl;
        }
    }
}
    
int main(int argc, const char **argv)
{
    char name[10] = "haoge";
    Studend first(10, name);
    Studend tow;
    tow = first;
    tow.Eq(first); 
    return 0;
}

6.常对象、常函数、常数据 const

编译规则决定, const 修饰的东西,不可以更改,只能引用

保护数据,函数,对象(编译器上了把锁)

	class Student
    {
    private:
        int id;
        char *name;
        const Sex sex;  // 常数据
    public:
        void ShowStudent() const // 常函数
        {
            cout << this->id << endl;
            cout << this->name << endl;
            cout << this->sex << endl;
        }
        void EqStudent(const Student &studend) //常对象
        {
            if (!strcmp(this->name, studend.name))
            {
                std::cout << "they are eq" << std::endl;
            }
            else
            {
                std::cout << "they are not eq" << std::endl;
            }
        }	

7.运算符重载

舒服了,代理运算符

#include <iostream>
using namespace std;
class Point
{
private:
	int x, y;

public:
	Point()
	{
		x = 0;
		y = 0;
	};
	Point(int newx, int newy);
	~Point();
	Point  operator =(const Point &c); //还可以通过友元运算符函数重载实现
	void disPoint();
};

Point::Point(int newx, int newy)
{
	x = newx;
	y = newy;
}
Point Point::operator=(const Point &c){
	this->x = c.x;
	this->y = c.y;
	return Point(this->x,this->y);
}

Point::~Point()
{
}
void Point::disPoint(){
	cout<<"x = "<<x ;
	cout<<"\ty = "<<y<<endl;
}

int main()
{
	Point a,b(1,2);
	(a=b).disPoint();
	return 0;
}


	result:
        x = 1   y = 2

8.类型重载

// 承接上部(7)代码

class Line
{
private:
	Point start ,end;
public:
	Line();
	Line(Point & ,Point &);
	~Line();
	operator Point();
};

Line::Line(Point &newStart,Point &newEnd) 
{	
	this->start = newStart;
	this->end = newEnd;
}

Line::~Line()
{
}
Line::operator Point()	// 类型重载
{
	return Point((this->start.x+this->end.x)/2,  // Line为friend 友元类
				(this->start.y+this->end.y)/2);
}

int main()
{
	Point a(1,3),b(3,5),c;
	Line d(a,b);
	c = d;
	c.disPoint();
	return 0;
}

 reslut:
	x = 2   y = 4

(c++)类与对象

原文:https://www.cnblogs.com/haoge2000/p/14119373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!