C++面向对象

类 & 对象

<font color="red">类是对象的抽象和概括,而对象是类的具体和实例</font>

<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=100% height=86 src="//music.163.com/outchain/player?type=2&id=1308818967&auto=1&height=66"></iframe>

  • 类用于指定对象的形式,它包含了数据表示法和用于处理数据的方法。
  • 类中的数据和方法称为类的成员。(成员有变量也有函数,分别成为类的属性和方法)
#include<iostream>
#include<Cstring>
using namespace std;

/*class Student
{
    public:
        int num;
        char name[100];
        int score;
        int print()
        {
            cout<<num<<" "<<name<<" "<<score;
            return 0;
        } 
};*/

class Student
{
    public: 
        int num;
        char name[100];
        int score;
        int print(); 
}; 

int Student::print()
{
    cout<<num<<" "<<name<<" "<<score;
    return 0; 
}
  
int main()
{
    Student A;
    A.num=1700710135;
    strcpy(A.name,"ye shan");
    A.score=100;
    //A.print();
    
    //Student *Ap;
    //Ap=&A;
    //Ap->print();
    
    Student& A_reference=A;  //引用定义时就要初始化 
    //A_reference=A;  错误 
    A_reference.print(); 
    
    return 0;
} 
image

类的定义

定义一个类,本质上是定义一个数据类型的蓝图。它定义类的对象包括了什么,以及可以在这个对象上执行哪些操作。

类的定义以关键字class开头,后面跟类的名称。类的主体包含在一对花括号中。类定义后必须跟着一个分号或一个声明列表

写法1

成员函数定义在在类里

class Student
{
    public:       //声明公有成员,可被类的任何对象访问
        int num;
        char name[100];
        int score;
        int print()
        {
            cout<<num<<" "<<name<<" "<<score;
            return 0;
        } 
};

写法2

成员函数定义在类外,使用范围解析运算符(作用域限定符)::
在::限定符前必须使用类名

class Student
{
    public:
        int num;
        char name[100];
        int score;
        int print(); 
}; 

int Student::print()
{
    cout<<num<<" "<<name<<" "<<score;
    return 0; 
}

对象的建立和使用

类就是包含函数的结构体,是一种自定义数据类型,用它定义出来变量,就是对象,这就是所谓的“对象是类的具体和实例”,定义了一个这个类的对象,也可以说实例化了一个对象,就是这个意思!

  1. 声明类的对象,就像声明基本的变量类型一样
  2. 访问公共数据成员可以使用直接成员访问运算符.来访问
Student A;    //声明A,类型为Student
A.num=1700710135;
strcpy(name,"ye shan");
A.score=100;
A.print();

类的访问修饰符

  • private
  • protected
  • public
  1. 成员和类的默认访问修饰符是private
      私有成员变量或函数在类的外部是不可访问的,甚至是不可查看的。只有类和友元函数可以访问私有成员。
  2. 保护成员修饰符protected,保护成员变量或函数与私有成员十分相似,但有一点不同,保护成员变量在派生类(即子类)中是可以访问的。
  3. 公有成员在程序中类的外部是可以访问的。可以在不适用任何成员函数来设置和获取公有变量的值
#include<iostream>
#include<Cstring>
using namespace std;
class Student
{
    private:
        int num;
    protected:
        int score;
    public:
        char name[100];
        int GetNum(int n);
        int GetScore(int s);
};

int Student::GetNum(int n)
{
    num=n;
    return num;
} 

int Student::GetScore(int s)
{
    score=s;
    return score;
}

int main()
{
    Student A;
    
    strcpy(A.name,"yeshan");
    cout<<"the name is"<<" "<<A.name<<endl;
    
    //A.num=1700710135,成员num是稀有的,不可这样用 
    cout<<"the num is"<<" "<<A.GetNum(1700710135)<<endl;
    
    cout<<"the score is"<<" "<<A.GetScore(100)<<endl;
    
    return 0; 
}
image

派生类中使用protected成员变量

#include<iostream>
#include<Cstring>
using namespace std;
class Student
{
    private:
        int num;
    protected:
        int score;
    public:
        char name[100];
        int GetNum(int n);
        int GetScore(int s);
};

class Small_Student:Student//Small_Student是派生类 
{
    public:
        int Get_Score_1(int temp); 
}; 
int Small_Student::Get_Score_1(int temp)   //子类成员函数 
{
    score=temp;
    return score;
} 

int Student::GetNum(int n)
{
    num=n;
    return num;
} 

int Student::GetScore(int s)
{
    score=s;
    return score;
}

int main()
{
    Student A;
    
    strcpy(A.name,"yeshan");
    cout<<"the name is"<<" "<<A.name<<endl;
    
    //A.num=1700710135,成员num是稀有的,不可这样用 
    cout<<"the num is"<<" "<<A.GetNum(1700710135)<<endl;
    
    cout<<"the score is"<<" "<<A.GetScore(100)<<endl;
    
    Small_Student B;
    cout<<"the score is"<<" "<<B.Get_Score_1(520)<<endl; 
    
    return 0; 
}
image
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • C++类和对象 C++ 在 C 语言的基础上增加了面向对象编程,C++ 支持面向对象程序设计。类是 C++ 的核心...
    863cda997e42阅读 3,902评论 0 4
  • C++面向对象编程 类与对象 定义一个类本质上是定义一个数据类型的蓝图 编译上述程序可以得到 类的成员函数 类的成...
    obsession_me阅读 3,682评论 0 0
  • 在面向对象编程中,类(Class)和对象(Object)是两个非常重要和基本的概念,类(Class)包含成员数据和...
    Sam_Lau阅读 10,386评论 5 18
  • 建立学习循环链条 确定学习目标 将目标不断分解,直到得出最小单位目标 形成“学习-反馈-改进”循环链条 小目标遇到...
    翼吱翅膀阅读 5,120评论 0 2
  • 我本来要安静下来,却总是有风吹起,我是一个人,不是一株木。我若如木无争,我还算一个人吗? 金发女郎在国际俱乐部的T...
    瓦力LY阅读 3,452评论 0 0