当前位置:网站首页>Experiment 4 operator overloading and virtual functions
Experiment 4 operator overloading and virtual functions
2022-07-19 06:36:00 【A student of programming】
- Experimental purpose or task
Object oriented software development method is a software development method developed by absorbing useful concepts and effective methods in the field of software engineering . It sets abstraction 、 encapsulation 、 Inheritance and polymorphism in one , It can help programmers develop modularity 、 High degree of data abstraction , Reflect information concealment 、 Reusable 、 Easy to modify 、 Easy to transplant and other features of the program .
This course is based on C++ The object-oriented mechanism in the language is mainly , Discuss the basic idea and method mechanism of object-oriented programming . Through the experimental teaching link , Enable learners to pass a certain amount of program examples and related exercises in the learning process , Gradually master C++ Process oriented and object-oriented functions , So as to master the basic knowledge and skills of object-oriented programming .
Through this experimental teaching link , It can improve learners' professional ability in the following aspects :
- Improve learners' abstract ability and logical thinking ability to solve problems ;
- Preliminarily master the object-oriented software design ideas ;
- Improve learners' self-learning ability of object-oriented program development tools ;
- Make learners have the basic literacy of object-oriented program development .
- Basic requirements of experimental teaching
- Understand and master C++ Advanced features of language functions , Understand the data abstraction and data encapsulation of object-oriented programming , master C++ The mechanism and method of data encapsulation and information hiding through classes and objects ;
- Understand the design idea of object-oriented programming code reuse , master C++ Class inheritance and derivation mechanism and implementation approach ;
- Understand the concept and idea of polymorphism in object-oriented programming , master C++ Operation overload 、 function overloading 、 The mechanism and method of realizing polymorphism by virtual function Technology ;
- Understand and master C++ I/O The idea and method of device independence of stream to realize input and output .
Summary
1. For classes MyString, Overload required ‘+’ Operator can be used to evaluate the expression :a=b+c; Represents the connection of two strings . among a,b,c Is the class MyString The object of .
2. Use virtual function programming to calculate the volume and surface area of sphere and cylinder . Because both sphere and cylinder can be regarded as inherited from circle , So you can define the circle class Circle As the base class . stay Circle Class to define a data member radius And two virtual functions area() and volume(). from Circle Class derivation Sphere Classes and Column class . For virtual functions in derived classes area() and volume() Redefinition , Calculate the volume and surface area of sphere and cylinder respectively .
The basic requirements
Overload the corresponding operators and write programs , Be able to use virtual functions to write programs, test and submit programs .
#include<iostream>
#include<cstring>
using namespace std;
class Mystring {
public:
Mystring(const char* ptr = "")
:_ptr(new char[strlen(ptr) + 1])
{
strcpy(_ptr, ptr);
}
Mystring operator+(Mystring& s) {
int len1 = strlen(_ptr);
int len2 = strlen(s._ptr);
char* tmp = new char[len1 + len2 + 1];
strcpy(tmp, _ptr);
strcpy(tmp + len1, s._ptr);
Mystring ret(tmp);
delete[] tmp;
return ret;
}
friend ostream& operator<<(ostream& _cout, Mystring& s);
private:
char* _ptr;
};
ostream& operator<<(ostream& _cout, Mystring& s) {
_cout << s._ptr;
return _cout;
}
int main() {
Mystring a("1324");
Mystring b("abcd");
Mystring c;
c = a + b;
cout << c << endl;
return 0;
}#include <iostream>
using namespace std;
double PI = 3.14156;
// round , Interface class
class Circle {
public:
// Surface area
virtual double area() = 0;
// Volume
virtual double volume() = 0;
protected:
// radius
double _r;
};
// The ball
class Sphere :public Circle {
public:
Sphere(double r = 0.0) {
_r = r;
}
// Surface area
virtual double area() {
return 4 * PI * _r * _r;
}
// Volume
virtual double volume() {
return 4.0/3 * PI * _r * _r * _r;
}
};
// Cylinder
class Column :public Circle {
public:
Column(double r = 0, double h = 0) {
_r = r;
_h = h;
}
// Surface area
virtual double area() {
return 2 * (PI * _r * _r) + (PI * 2 * _r * _h);
}
// Volume
virtual double volume() {
return PI * _r * _r * _h;
}
private:
// high
double _h;
};
int main() {
double rS, rC, h;
cout << " Please enter the radius of the sphere :";
cin >> rS;
cout << " Please enter the radius of the cylinder :";
cin >> rC;
cout << " Please enter the height of the cylinder :";
cin >> h;
cout << endl;
Sphere sphere(rS);
Column column(rC, h);
cout << " The radius of the sphere is :" << rS << endl;
cout << " The surface area of the sphere is :" << sphere.area() << endl;
cout << " The volume of the sphere is :" << sphere.volume() << endl << endl;
cout << " The radius of the cylinder is :" << rC << endl;
cout << " The surface area of the cylinder is :" << column.area() << endl;
cout << " The volume of the cylinder is :" << column.volume() << endl << endl;
return 0;
}边栏推荐
- 虚拟现实中的眼睛跟踪
- 用头部运动学习无姿态注视偏差
- Operation of documents in index library
- Markdown syntax and common shortcuts
- 浅谈跨域的几种解决方案
- 2022/07/10 group 5 Ding Shuai's study notes day03
- 读取图片 进行空间转换 展现不同颜色空间
- DSL realizes automatic completion query
- Automatic completion & (custom) Pinyin word Separator &
- Robot stitching gesture recognition and classification
猜你喜欢
随机推荐
用头部运动学习无姿态注视偏差
感知智能手机上用户的关注状态
Depth first search (DFS for short)
面试复习第N次
Volatile function of embedded C language
Solutions to slow transmission speed of FileZilla virtual machine
你的企业最适合哪种深度学习?
Make menuconfig missing ncurses
DSL realizes automatic completion query
Open source online markdown editor -- [editor.md]
Google browser cannot modify cookies manually, and cookies are marked red
Make config analysis of configuration commands before uboot compilation
斑点检测 记录
通过VOR深度估计解决三维注视交互中的目标模糊问题
Unity2D学习 Fox Game制作 过程1:基本的游戏角色控制,动画效果,镜头控制,物品收集,bug优化
通過VOR深度估計解决三維注視交互中的目標模糊問題
《PyTorch深度学习实践》-B站 刘二大人-day7
[force buckle] ring list II
山西省第二届网络安全技能大赛(企业组)部分赛题WP(三)
MEX and Increments








