【COMP282 LEC 1-2】

LEC 1 Introduction to c++

Header file :

1. 不用写 " .h "

Function : 

数据类型有string了,就像java,需要#include<string>

Output system :

Explain by example:

std::cout << hello << " is of length " << x;

std::cout means standard character output

<< means insertion (so we insert into std::cout) – each of them inserts into the left most

  It must be by types supporting <<

all standard types does support << and we will later in the course see how to add this feature in our own classes

The rest are strings or numbers We can insert as many as we would like

It looks better than printf, because we do not need to have that format string first

Namespace

std::bla says that bla is in the standard namespace

Namespace is like packages in java

E.g. std::cout means cout as it is defined in the std namespace and std::endl means endl as it is defined in the std namespace

Input system : 

Input is done by writing std::cin >> bla;

  When we have a std::string called bla

  It waits until a line has been written and insert the part upto the first space in bla

LEC 2

C++ classes

>> Classes in C++ is an option – not a requirement

>> If you do not like classes, you can simply not write any in C++

  You can still get things like nicer strings and/or nicer input in/output

  Some small programs are written like that…

  Take the features you like, avoid the features you do not…

就像上面那张图一样,class的后括号要加一个分号,这一点不太一样

 Access modifiers in C++

>> There are three access modifiers in C++ : private(default), public, protected

>> Most is the same as Java

>>The syntax is different from Java: You write e.g. public: and then a list of declarations and they are all public 不是写在类的那一行的前面,而是在括号里面

Example : 

Getters and Setters 

Generally we don’t want to expose internal variables to the outside world, so we keep them private and define getters and setters

   >> We do not actually want to expose getters and setters either, except for cases (like here), where we are using the class directly for data storage

Constructors and Destructors

Constructors in C++ behave the same as they do in Java

  >> Called whenever an object is created  

  >> Has no return type and must have the same name as the class

  >> Performs any setup required by the class (such as memory allocation) 

  >> If you don’t declare a constructor then a default one will be used

  >> If you do declare one, the default one will get removed

Java does not have destructors because it uses garbage collection

  >> Objects are automatically deleted when no longer in use

  >> Memory is freed and tidied up automatically

In C++ the destructor is called whenever an object is deleted

  >> Has the same name as the class with a tilde in front of it

  >> Should free up any memory and resources allocated by the constructor 应该释放构造函数分配的内存和资源

Memory Management in C++

Member Initialisation

Inheritence

Like in Java, but with different syntax

Child classes inherit variables and methods of their parent

Difference: There are no interfaces though 没有接口

Difference: C++ has access modifiers on inheritance  访问修饰符

 

Polymorphism(多态性):

首先dog类继承了父类animal类,他继承了父类的方法“makeNoise”。在main里,创建了一个dog类叫“rover”

?????

Virtual Polymorphism: 

Remember that C++ is used for coding low-level device drivers, embedded systems, etc.

1. Needs to generate small, memory efficient, fast executable code

2. Polymorphism adds complexity and size to the finished product

3. So, the compiler only does it when we explicitly ask for it

Initialisation:

 

 

Slicing:分割

1. 定义了一个方法:“output” 作用就是打印出一个格式,比如说这里就是

Fluffy weighs 5 and has lives 9

2. 把子类的data(fluffy) pass to 了父类animal(pet)

Accessing child through parent: 

The three ways to pass objects to functions: 

 

结果:

rover weighs 8

fluffy weighs 0

Permanent objects:​​​​​​​ 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: COBOL中,COMPCOMP-3都是数据类型,但它们有一些区别。 COMP是一种二进制数据类型,它占用一个字(4个字节)的存储空间。它可以存储整数、小数和浮点数等数据类型。但是,它不能存储带有小数点的数值。 COMP-3是一种压缩的二进制数据类型,它占用不定长的存储空间。它可以存储整数、小数和浮点数等数据类型,并且可以存储带有小数点的数值。但是,它的存储方式比较特殊,需要进行压缩和解压缩操作,所以在处理时需要特别注意。 因此,如果需要存储带有小数点的数值,建议使用COMP-3类型。如果只需要存储整数、小数和浮点数等数据类型,可以使用COMP类型。 ### 回答2: COBOL(通用商务语言,英文全称:Common Business-Oriented Language)是一种广泛用于企业应用的编程语言。在COBOL中,主要有两种数据类型:二进制和十进制。 在二进制数据类型中,COBOL中有两个最主要的数据类型── COMPCOMP-3。这两个数据类型的区别可以从编程语言的使用和存储方面来阐述。 首先,COMP(有时称为COMP-1)是一种二进制数据类型,它存储的是32位或64位的二进制数。它最初是用来存储小数的数值,但它还可以用于存储整数。在运算时,二进制数被转换为十进制数,在计算结束时再转换回二进制数。这种数据类型通常用于存储非常大的数字,因为二进制位数越多,数值就越大。如果您的应用程序需要处理非常大的数字,那么COMP类型是非常有用的。 其次,COMP-3(也称为Packed-Decimal)是一种可压缩的二进制数据类型,它可以存储数字和数值。它被广泛用于金融应用程序中,因为它可以存储小数点位置并且可以通过使用压缩算法来减少存储空间。该算法通过将数字加上13(十进制),然后将结果除以10,并将余数加入表示数字长度的字段中,来将数字压缩成不同的字节数。例如,一个俩位的数字在COMP-3中只需要1个字节,而一个四位数字需要2个字节,并且这个数字只能使用10进制表示。 总之,COBOL中的COMPCOMP-3数据类型的区别在于它们存储数据的方式,以及数据类型的存储大小。COMP主要用于存储非常大的数字,而COMP-3则用于存储金融应用程序中的数字和数值,通过压缩算法来节约存储空间。需要根据具体的应用场景来选择使用哪种数据类型。 ### 回答3: COBOL是一种面向商业应用的高级编程语言。在使用COBOL时,我们常会听到两个概念:compcomp-3。这两者有什么区别?下面就来一一讲解: 1. comp(二进制) comp是COBOL中的一种数据类型,它表示二进制数。comp所占据的存储空间是由所存储数值的位数决定的,例如:pic S9(9) comp 表示一个9位带符号的二进制数。 使用comp类型变量的好处是它占用的存储空间少,节省了系统资源。它也更适合于整数计算,被广泛用于所有的计算机中(从小到大,64位计算机除外)。 2. comp-3(压缩型二进制) COBOL语言中,comp-3也是一种数据类型,它表示一种压缩型二进制数。它将数字压缩成BCD码表现,占据的存储空间比comp要少得多。 同时,它也更适用于金额计算或者其他金融计算。尤其是在大型银行和金融机构中,comp-3被广泛使用。 总结: - comp是一个二进制数据类型,它占用比较小的存储空间,适用于整数计算。 - comp-3是一种压缩型二进制数据类型,它将数字压缩成BCD码表现,占据的存储空间比comp要少得多,适用于金额等金融计算。 根据实际需求来选择不同的存储方式是非常重要的,能够提高应用程序的执行效率和节省系统资源。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值