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指针
当成员函数的形参名与该类的成员变量相同时候,该如何区分? ----------------------------必须用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 定义
5.2 公有派生类
- C 编程第1篇---开篇介绍
- C++篇----开篇(1)
- 1、C语言和设计模式(之开篇)
- (1)开篇
- JavaScript(1)开篇
- 数据结构基础篇(1)--开篇
- 第1篇-JAVA开篇
- C++篇----开篇(2)
- C++篇-----开篇(3)
- C++篇---开篇(4)
- php学习(1)开篇
- HTML学习(1)开篇
- CSS学习(1)开篇
- XHTML学习(1)开篇
- 存储过程(1)----开篇
- C笔记---开篇
- 开篇---c 复习小结
- C++grammer开篇
- kafka跟storm收集日志解决方案
- swap file “*.swp”already exists
- MJPEG协议入门介绍
- 学习JavaScript设计模式(六)
- linux 端口被占用解决
- C++篇----开篇(1)
- java基础_day0022_java关键字_this_super_构造器_子类对象实例化
- nswoj G
- Hadoop2.X中Mapreduce,YARN等配置,启动和常见错误解决办法
- for循环,定时器,闭包混合一块的那点事。
- 用JavaScript生成验证码
- js 获取复选框被选中的值 jquery
- Linux中的文件权限
- JSP动作元素