C++篇----开篇(1)

来源:互联网 发布:初中考试软件 编辑:程序博客网 时间:2024/06/05 17:06

知识点一:类

类描述了一组有相同特性(数据元素)和相同行为(函数)的对象。

注意:类只是一种数据类型,系统并不会为其分配内存空间,所以定义类的数据成员时,不能够对其初始化。



创建第一个类:

通过类向导生成student.h和student.cpp两个文件

student.h文件

#pragma once
class student
{
public:
student(char* myName,char* myNum,float myScore); //构造函数
~student();
private:
char name[10];
char strNum[10];
float mathScore;
public:
char* getName();
char* getNum();
float getScore();  
void  setName(char* myName);
void  setNum(char* myNum);
void  setScore(float myScore);
};


student.cpp文件

#include "student.h"
#include "string.h"

student::student(char* myName, char* myNum, float myScore)
{
strcpy(name,myName);
strcpy(strNum, myNum);
mathScore = myScore;
}

student::~student(void)
{
}
char* student::getName()
{
return name;
}
char* student::getNum()
{
return strNum;
}
float student::getScore()
{
return  mathScore;
}
void student::setName(char* myName)
{
strcpy(name,myName);
}


void student::setNum(char* myNum)
{
strcpy(strNum,myNum);
}
void student::setScore(float myScore)
{
mathScore = myScore;
}


主函数:

#include<iostream>
#include"student.h"
using namespace std;
int main()
{
//student stu;
//stu.setName("张三");
//stu.setNum("54");
//stu.setScore(93.5); //创建对象stu 初始化方式一

student stu("张三","54",93.5);//直接通过构造函数对对象初始化


cout << "姓名: " << stu.getName() << "学号: " << stu.getNum() << "分数: " << stu.getScore() << endl;
system("pause");
return 0;
}

知识点二: 对象

注意几点:1. 在定义对象时候,应该尽量在声明类之后定义; 2. 一个类对象只能访问该类的公有成员;3. 使用.(点)访问对象的成员函数和成员变量


2.1 类的指针

     student stu;

     student*  c_stu =&stu //类的指针,可以通过指针调用对象内的成员,c_stu->getName();

2.2 使用new定义对象

     student* c_stu; //定义一个类的指针

      c_stu=new student();

    使用delete释放对象

     delete c_stu;   //释放对象的指针

      c_stu=NULL;

2.3 类的对象数组

   student stu[3]={student("lin","01",90),student("wang","02",80),student("chen","03",60)}

  for(int i=0;i<3;i++)

{

   cout<<stu[i].getName()<<"  ";

}

2.4  const成员变量

使用const关键字申明的成员变量,成为常成员变量。

注意:1 常成员变量的值是不能更新的;2 常成员变量必须进行初始化,初始化只能够通过构造函数中的成员初始化列表的方式

原本是

student::student(char* myName, char* myNum, float myScore)
{
strcpy(name,myName);
strcpy(strNum, myNum);
mathScore = myScore;
}

如果我们这里将mathScore设置成常成员变量(记得在student.h中改为const float mathScore):

那么在构造函数中需要如下修改

student::student(char* myName, char* myNum, float myScore):mathScore(myScore)
{
strcpy(name,myName);
strcpy(strNum, myNum);
}

   常成员函数

     1.常成员函数不能修改任何成员变量;2.常成员函数中不能调用非const成员函数;

    常对象

    1定义时候必须初始化,2 而且只能调用常成员函数

 

知识点三:构造函数和析构函数

3.1 构造函数定义:与类同名的成员函数就是构造函数;

       构造函数主要用途: 处理对象初始化

        例如上例:加粗部分表示通过构造函数直接对对象进行初始化


3.2 析构函数:

析构函数与构造函数相反,是在对象的生命期结束时,自动释放系统为对象所分配的空间,也就是撤销一个对象;  


知识点四:this指针

  1.this指针是类的一个自动生成,自动隐藏的私有成员,它存在于类的非静态成员函数中,指向被调用函数所在的对象
  2.全局仅有一个this指针,当一个对象被创建时候,this指针就存放指向对象数据的首地址。

当成员函数的形参名与该类的成员变量相同时候,该如何区分? ----------------------------必须用this指针来区分

/**************************Mypoint.h********************************************/

#pragma once
class CMypoint
{
public:
CMypoint(int x,int y);
~CMypoint();
public:
int x, y;
public:
void copy(CMypoint pt);
};

/*********************************Mypoint.cpp*********************************/

#include "Mypoint.h"
CMypoint::CMypoint(int x,int y)
{
this->x = x;
this->y = y;
}
CMypoint::~CMypoint()
{
}
void CMypoint::copy(CMypoint pt)
{
*this = pt;
}

/********************************pointTest.CPP...................................................../

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


int main()
{
CMypoint pt(2,3);
CMypoint pt1(0, 0);


pt1.copy(pt);
cout << pt1.x << "," << pt1.y << endl;
system("pause");
return 0;
}

知识点五:继承和派生

5.1 定义

当一个新类从一个已经定义的类中派生后,新类不仅继承了原有类的属性和方法(继承),并且拥有了自己新的属性和方法(派生)
比如:定义一个学生的类,这个学生继承了人这个类的所有属性(继承)
          而学生这个类还具有自己特有的属性,比如说学号(派生)

类的继承具有的特性1.单向性; 2.传递性 ;3.可重用性

5.2  公有派生类





公有派生类可以访问基类的public和protected.
而派生类对象只能够访问基类的public


5.3 私有派生类



注意:私有派生类中,基类的公有成员,保护成员,和私有成员的访问属性都将变成私有,且基类的私有成员在派生类中被隐藏
            私有派生类的对象只能访问派生类的公有成员,不能够访问基类的任何成员。

5.4 保护派生类


注意:保护派生类中,基类的公有成员,保护成员和私有成员的访问属性都将变为保护。且基类的私有成员在派生类中被隐藏
            保护派生类的对象只能访问派生类的公有成员,不能够访问基类的任何成员。
0 0
原创粉丝点击