结构体,class

struct没有继承,没有封装,要说封装只有初步封装。而class把数据,接口可以以三种类型封装,private,public,protected;还可以继承和派生。

它们都可以提供自己的接口函数,构造函数。一个类可以由结构继承而来。struct只能叫做数据的集合,外部可以任意访问,但是类就完成了封装,维护了数据安全,这就是面向对象的理念。

struct 

结构体的基本定义和初始化:

#include <iostream>
#include <string>
using namespace std;

struct STU
{
    STU(int x,char y):age(x),name(y){}  //构造函数,初始化结构体内成员的值,这里在{}中书写age=x,name=y也是等效果的
    int age;
    char name;    //结构体内的成员
};

struct STU2
{
    int age;
    char name;    //结构体内的成员
}temp2;   //可以直接在结构体最后声明变量但是就不能使用自定义构造函数

int main(int argc, char *argv[])
{
    STU temp(1,'q');  //声明一个结构体变量
    cout<<temp.age<<" "<<temp.name<<endl; 
    temp2.age=11;
    temp2.name='w';
    cout<<temp2.age<<" "<<temp2.name<<endl; 
    return 0;
}

class

参考:c++ class基础知识 - 盖世无双 - 博客园

public: 可访问范围: 类内部, 类外部, 派生类内部.
private: 可访问范围: 类内部
protected:可访问范围: 类内部, 派生类内部

一般操作:

1 将所有变量声明为private, 外部要访问时只能通过set函数和get函数.

2 将外部需要使用的函数声明为public, 其余都声明为private.

创建、访问、指针、类方法

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

class Student{
    public:
        char *name;
        int age;
        float score;

        void say1(){  //类方法
            cout <<"say1:"<< "name=" << name << ", age=" << age << ", score=" << score << endl;
        }

        // 类方法定义在类内部会自动成为内联函数, 一般情况下这样不好, 所以类方法一般都定义在类体外部.
        void say2();
};

void Student::say2(){
  cout<<"say2:"<< "name=" << name << ", age=" << age << ", score=" << score << endl;
}

int main(int argc, char *argv[])
{
  //声明和访问类成员
  Student stu1;  
  stu1.say1();
  stu1.name = "小明";
  stu1.age=12;
  stu1.score=100;
  stu1.say2();

  // 指针用法
  Student stu3;
  Student *p=&stu3;
  // Student *p=new Student; //空指针
  p->age=13;
  p->name="小红";
  p->say1();

  return 0;
}

构造函数

构造函数需要是public的.
构造函数允许重载.
构造函数可以使用初始化列表形式对成员变量赋值:

Student::Student(char *name, int age, float score): m_name(name), m_age(age), m_score(score){
//TODO;
}

构造函数只能有一个

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

class Student{
    public:
        char *name;
        int age;
        float score;

        Student(char *name,int age,int score){   //构造函数初始化
          this->age=age;
          this->score=score;
          this->name=name;
        }

        void say1(){
            cout <<"say1:"<< "name=" << name << ", age=" << age << ", score=" << score << endl;
        }
};

int main(int argc, char *argv[])
{
  Student stu1("小明",10,99);  
  stu1.say1();
  return 0;
}

构造函数也可以像上面的代码中类方法类似的放在class外部,但是不同的是不需要返回类型


#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

class Student{
    public:
        char *name;
        int age;
        float score;

        Student();
        void say1();
};

void Student::say1(){
  cout <<"say1:"<< "name=" << name << ", age=" << age << ", score=" << score << endl;    
}

Student::Student(){
  this->age=0;
  this->score=0;
  name="0";    //此处this可以使用也可以不使用
}

int main(int argc, char *argv[])
{
  Student stu1;  
  stu1.say1();
  return 0;
}

析构函数

析构函数于构造函数相对应,构造函数是对象创建的时候自动调用的,而析构函数就是对象在销毁的时候自动调用的的。

构造函数可以有参数,但析构函数不能有参数

与构造函数相同的是,如果我们没有显式的写出析构函数,那么编译器也会自动的给我们加上一个析构函数,什么都不做;如果我们显式的写了析构函数,那么将会覆盖默认的析构函数

在主函数中,析构函数的执行在return语句之前,这也说明主函数结束的标志是return,return执行完后主函数也就执行完了,就算return后面还有其他的语句,也不会执行的。

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

class Student
{
public:
	Student()
	{
		cout << "Beginning" << endl;
	}
	~Student()
	{
		cout << "End" << endl;
	}
};
 
int main()
{
  Student stu;
  getchar();
	return 0;
}

在getchar()之后才return 0之前执行了析构函数。

注:指针对象运行到主函数结束,析构函数也不会被执行,只有使用delete(指针)才会触发析构函数。

静态变量、静态函数

静态变量:

static成员变量不占用对象的内存, 而是在对象之外开辟内存.

#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;

class Student
{
  public:
    static int num;  //静态变量
    void say(){
      cout<<num<<endl;
    }
};
int Student::num=0;  //这里变量的初始化需要放在class声明之后
int main()
{
  Student stu;
  Student *p=&stu;
  stu.say();
  cout<<Student::num<<" "<<stu.num<<" "<<p->num<<endl;  //可以通过类、对象、指针来访问
	return 0;
}

静态函数:

普通函数可以访问所有变量和函数,静态函数只能访问静态变量和静态函数.
静态函数中没有形参this.
静态函数在声明时要加上static, 定义时不用加static.
静态函数可以通过类来调用, 也可以通过对象调用.

const变量和const函数

const成员变量的用法和普通const变量的用法相似, 只能通过构造函数初始化列表进行初始化.
const成员函数可以使用类中所有变量, 但不能修改它们的值, 比如get函数.
const成员函数在声明和定义时都要加const关键字:

//声明
public:
    char *getname() const;
    int getage() const;
    float getscore() const;

//定义
char * Student::getname() const {
    return m_name;
}
int Student::getage() const {
    return m_age;
}
float Student::getscore() const {
    return m_score;
}

//注意: const位置不同代表不同的含义
// const char *getname(); //表示返回值是const.
// char *getname() const; //表示函数是const, 即函数中不能修改类成员.

友元

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值