C++面向对象程序设计 - 创建学生类

        在20世纪80年代提出了面向对象的程序设计(Object oriented programming, OOP)思想,在此形势下,C++由AT&TBell(贝尔)实验室于20世纪80年代初在C语言的基础上开发成功,C++保留了C语言原有的所有优点,增加了面向对象的机制。为了强调它是C语言的增强版本,用了C语言的自加运算符“++”,改称为C++。

        如果你有Java、C#、PHP、NodeJs等语言基础,再来了解C++,会发现它们在面向对象编程方面有许多相似之处,有封装、继承、多态、类与对象、访问个修饰符等。

        这里通过一个简单的程序,了解下C++对象是如何声明和实例的。

一、实例学生类

        在C++中,输出流cout必须和输出运算符“<<”一起使用,"<<"在这里不是作为位运算的左移运算符,而是起插入的作用。标准输入流cin是从键盘向内存流动的数据,用">>"运算符从输入设备键盘取得数据送到输入流cin中。

#include <iostream>		//下面用到的cin输入和cout输出时需要用到此头文件
using namespace std;	//使用命名空间std
/**
 * 学生类
 */
class Student{
	// 定义私有属性
	private:
		char name[10];
		int num;
		int score;
	// 定义对外函数
	public:
		// 输入数据
		void setdata(){
			cin >> name;
			cin >> num;
			cin >> score;
		}
		// 显示内容
		void display(){
			cout << "- - - - - - - - - - - - - - - - - - - - - - " << endl;
			cout << "name:" << name << endl;
			cout << "num=" << num << endl;
			cout << "score=" << score << endl;
		};
};

// 定义Student对象变量stud1, stud2
Student stud1, stud2;

int main(){
	// 输入姓名、学号、成绩
	stud1.setdata();
	stud2.setdata();
	// 输出结果
	stud1.display();
	stud2.display();
}

        编译结果如下:

        运行结果如下:

二、比较成绩

        增加maxScore()函数,用来比较学生间成绩最高分,代码如下:

#include <iostream>		//下面用到的cin输入和cout输出时需要用到此头文件
using namespace std;	//使用命名空间std
/**
 * 学生类
 */
class Student{
	// 定义私有属性
	private:
		char name[10];
		int num;
		int score;
	// 定义对外函数
	public:
		// 输入数据
		void setdata(){
			cin >> name;
			cin >> num;
			cin >> score;
		}
		// 显示内容
		void display(){
			cout << "- - - - - - - - - - - - - - - - - - - - - - " << endl;
			cout << "name:" << name << endl;
			cout << "num=" << num << endl;
			cout << "score=" << score << endl;
		}
		// 获取成绩
		int getScore(){
			return score;
		}
		// 获取姓名
		char* getName(){
			return name;
		};
};

// 判断成绩大小
Student maxScore(Student s1, Student s2){
	return s1.getScore() > s2.getScore() ? s1 : s2;
}

// 定义Student对象变量stud1, stud2
Student stud1, stud2;

int main(){
	// 输入姓名、学号、成绩
	stud1.setdata();
	stud2.setdata();
	// 输出结果
	stud1.display();
	stud2.display();
	// 计算最高成绩
	Student ms = maxScore(stud1, stud2);
	cout << "- - - - - - - - - - - - - - - - - - - - - - " << endl;
	cout << ms.getName() << " had the highest score with a score of " << ms.getScore() << endl;
}

        编译结果如下:

        运行结果如下:

三、char和char*区别

        char和char*在C++中有显著的区别,主要体现在数据类型、内存占用、值和用途以及字符串的表示等方面。char和char*在C++中各有其独特的用途和特性,理解它们的区别对于正确编写和使用C++程序至关重要。

  • char是C++中的基本数据类型,表示一个字符,它占据一个字节的内存空间,可以存储ASCII码表中的任意一个字符。
  • char*通常用于表示字符串或字符数组的起始位置,指向一个以空字符('\0')结尾的字符数组,这样就可以表示一个字符串。

错误写法(会报错误:[Error] invalid conversion from 'char*' to 'char' [-fpermissive]):

char getName(){
    return name;
};

正确写法:

char* getName(){
    return name;
};

四、右对齐

        要想实现内容右对齐,可以使用控制符setw设置(注意:若使用setw,必须包含头文件iomanip),否则会报错:[Error] 'setw' was not declared in this scope; did you mean 'getw'?,错误中已提示当前此范围内未声明setw,通过#include引入即可。

        另外,若超出设置设置列数,则按实际长度输出。

        代码如下:

#include <iostream>		//下面用到的cin输入和cout输出时需要用到此头文件
#include <iomanip>		// setw设置时需要用到此头文件
using namespace std;	//使用命名空间std
/**
 * 学生类
 */
class Student{
	// 定义私有属性
	private:
		char name[10];
		int num;
		int score;
	// 定义对外函数
	public:
		// 输入数据
		void setdata(){
			cin >> name;
			cin >> num;
			cin >> score;
		}
		// 显示内容
		void display(){
			cout << "- - - - - - - - - - - - - - - - - - - - - - " << endl;
			cout << setw(6) << "name:" << setw(8) << name << endl;
			cout << setw(6) << "num=" << setw(8) << num << endl;
			cout << setw(6) << "score=" << setw(8) << score << endl;
		}
		// 获取成绩
		int getScore(){
			return score;
		}
		// 获取姓名
		char* getName(){
			return name;
		};
};

// 判断成绩大小
Student maxScore(Student s1, Student s2){
	return s1.getScore() > s2.getScore() ? s1 : s2;
}

// 定义Student对象变量stud1, stud2
Student stud1, stud2;

int main(){
	// 输入姓名、学号、成绩
	stud1.setdata();
	stud2.setdata();
	// 输出结果
	stud1.display();
	stud2.display();
	// 计算最高成绩
	Student ms = maxScore(stud1, stud2);
	cout << "- - - - - - - - - - - - - - - - - - - - - - " << endl;
	cout << ms.getName() << " had the highest score with a score of " << ms.getScore() << endl;
}

        编译结果如下:

        运行结果如下:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值