基本概念
1a general-purpose programming language
用于创建计算机程序。艺术类app音乐播放器甚至视频游戏。
2实例
include <iostream>//头文件#标志是编译器预先处理的开始。#include表示预处 理头文件 包含在内iostream表示标准的输入输出流数据,streams用于执行输入和输出操作
using namespace std;//告诉编译器使用标准命名空间
//c++的可读性和结构为:空格,换行,新行都是忽略的,知识为了视觉舒服。
int main()//函数本体部分,用于描述将要被执行的程序
{//区块必须是一堆括号;
cout<<"Hello world!";
return 0;//结束main()函数,使它返回值0;若非零1出现,则结束不正常
}
结束main()函数, calling process调用过程
2.0
endl move to a new line;
alternative: \n
\n\n means two lines
//this is comment
/this is comment/
2.1Why do you have to place return 0; at the end of your code? what is it's purpose?:
Technically, in C or C++ main function has to return a value because it is declared as "int main" which means "main function should return integer data type" if main is declared like "void main", then there's no need of return 0.Some compilers even accept and compile the code even if u dont write return 0. varies from compiler to compiler.void main is not allowed by the C++ standard (nor the C standard) and should not even compile.
The int value that main returns is usually the value that will be passed back to the operating system. 0 traditionally indicates that the program was successful.
You don't have to return 0 explicitly, because that'll happen automatically when main terminates. But it's important to keep in mind that main is the only function where omitting return is allowed.
3c++的编译环境建设IDE:
Getting the tools :reference this lesson if you need to install the software on your computer
free available complier: GNU C/C++ complier
a free tool Code::Blocks, which include both an IDE(provides tools to writing source code ,any text editor can be used as an IDE, Integrated Development Environment) and a complier and is available for windows ,linux and MacOs
then to create a project open file--new--project-console application --go
4variable identifier标识符(标识符命名规则,人以字母或underscore或者数字)
identifier is a name for a variable ,function, class , module, or any the user-defined item.
variables must have a data type/must be declared before their use
cout cin: take information (data) from the user a data type be mentioned once for a variable there must be a function named main each variable must have its data type.
一个赋值问题 int x = 3, y = 2 ;
y = x++;//先赋值给y,然后x的值加1。这一行改变y 的值=3,
cout << ++y;//4--->increments the value y//++在这里也不起作用,因为它只有在当y是一个变量(容器)的时候(如下面5的情况才管用,现在他只是一个容器装着的仍是3)
只有这种情况下是5
int x =3,y = 2 ;
int y=++x;//先加1赋值给x,然后给y
cout << ++y;//5
下面的情况是4:
int x = 3;
int y = x ++;//先赋值给y,然后x加1
cout << ++y;//4
条件与循环
1if(10 == 10){
cout<< "yes";
}
if(10 != 10){
cout<< "yes";
}
int mark = 90;
if(mark < 50){
if(mark < 45){
cout << "You failed."<<endl;
cout << "Sorry."<< endl;
}
}
you can nested as many as you want in an if statement .
if(condition){
//statements
}else{
//statements
}
2while(condition){
//statements;
}
int num = 1;
while(num < 6){
cout << "Number:"<< num <<endl;
num ++;
}
int num = 1;
int number;
while(num <= 5){
cin >> number;
num++;
}
int x = 1;
int number;
int total = 0;
while(x <= 5){
cin >> number;
total = total + number;
x++;
}
cout << "Sum:" << total << endl;
3int main()
{
for(a=0;a<10;a++){
cout << a << endl;}
return 0;
}
4do{
statements;
}while(condition);
int a = 0;
do{
cout<<a<<endl;
a++;
}while(a<5);
5int age = 42;
if(age == 16){
cout<<"Too young";
}
if(age == 42){
cout<<"Adult";
}
if(age == 70){
cout<<"Senior";
}
int age = 25;
switch(age){
case 16:
cout<<"Too young";
break;
case 42:
cout<<"Adult";
break;
case 70:
cout<<"Senior";
break;
default:
cout<<"This is the default case";
}
//break如果省去,结果出现所有结果
/Adult
Senior
This is the default case/
6逻辑符号
int age = 16;
int score = 90;
if(age>20||score<50){
cout<< "Accepted!"<<endl;
}
int age = 20;
int grade = 80;
if(age>16&&age<60&&grade>50){
cout<< "Accepted!"<<endl;
}
int age = 10;
if(!(age>16)){
cout<< "Your age is less than 16"<<endl;
}
数据类型,数组,指针
1Data Types are the proper use of an identifier( identifier标识符)
int,integers 55+15
int string 55+"John" illegal不合法,整型与字符串不能相加
“Hello,”+“John”合法
floating point vs integers------3.14,-21.23 vs -7,42
strings &Characters single quotation marks indicate a character;double quotes create a string literal ----character ‘a’ & string “a”
booleans: true (1)or false(0)
2integer type according its size varies from 4 bytes the minimum size to 32 bytes
signed int:hold both negative and positive numbers----e.g-42&42
unsigned int:only positive values ----e.g42
short int:half of the default size 默认大小的一半
long int:twice of the default size 默认大小的一倍
3float piont can hold a real number,e.g 420.22,-3.45,0.00214
float: 4 bytes
double:8 bytes
long double:a double 8 bytes or 16 bytes
4string is an ordered sequence of characters,closed in double quotation marks
we need to include the <string>library to use the string data type(library like tools you need to repair a bike)
include<string>
using namespace std;
int main(){
string a = "I am learning C++";
return 0;
}
5characters
char variable holds a 1-byte integer, but it's not a integer, the value of a char variable is typically interpreted as an ASCII character(American Standard Code for Information Interchange is a character-encoding 字符编码scheme 计划,used to represent text in computers), it's enclosed between single quotes e.g 'a'
ASCII值 其实就是编号 给每个常用字符(英文字母,各种符号)编号,ASCII规定了127个字符。 这些编号可以对应转换成2进制的数(例如:64=2的6次方 可以写作:01000000)。现在的计算机处理的都是前面举的2进制数的例子。
计算机内部一切工作全是以数字代码的方式展开的,A这个按键的代码就是一个ASCII码,它的二进制形式是01000001,计算机一旦收到01000001,就认为你按了一下A键,再收到00100000(空格键ASCII码),就认为你按了空格键。一句话,计算机通过收到的ASCII码判断你按了什么键,然后做出决定去进行相关的工作。
或者如果计算机要把你都按了什么键,做了什么操作存储记录下来,那么把这些ASCII码按顺序记录就可以了。
6变量命名规则
字母和"_"开头,只能这两种。后面大小写都可以。
大小写区分不一样
特殊词不能用于命名,如int,float,double,cout
6数组
1int a[5];
int b[5] = {11,34,45,26,73};
花括号里的数目不能超过方括号里的数字。
[0]b表示第一个元素,size[9]=10elements
int b[]={12,334,34,53,46};
cout<<b[0]<<endl;//12
int b[]={12,334,34,53,46};
b[2]=51;//分配值51给第三个元素
7数组循环
int myArr[5];
for(int x=0;x<5;x++){
myArr[x]=42;
cout<<x<<":"<<myArr[x]<<endl;
}
8数组运算
int arr[]={11,24,35,111,235};
int sum=0;
for(int x=0;x<5;x++){
sum+=arr[x];
}
cout<<sum<<endl;//416
9多维数组
int x[1][2]表示第一排第二列,
int x[2][3]={
{2,3,4},//第一排
{3,4,5}//第二排
};
也可以写成:int x[2][3]={{2,3,4},{3,4,5}};
找到某矩阵中的某一个元素x[1][2]
int x[2][3]={{2,3,4},{3,4,5}};
cout<<x[1][2]<<endl;//5
cout<<x[0][2]<<endl;//4
int arr[2][3]={{2,3,4},{3,4,5}};//两排三列
cin>>arr[1][1];
10指针
每一个变量都有一个内存地址,memory location,定义的地址,address defined
通过ampersand (&)来找到地址denote意味着一个内存中的地址。指针就是一个含有内存地址的数据类型
内存是计算机中重要的部件之一,它是与CPU进行沟通的桥梁。计算机中所有程序的运行都是在内存中进行的,因此内存的性能对计算机的影响非常大。 内存(Memory)也被称为内存储器,其作用是用于暂时存放CPU中的运算数据,以及与硬盘等外部存储器交换的数据。只要计算机在运行中,CPU就会把需要运算的数据调到内存中进行运算,当运算完成后CPU再将结果传送出来,内存的运行也决定了计算机的稳定运行。
int score = 5;
cout<<&score<<endl;//0x7fff516b055c---this outputs the memory address ,which stores the variable score.输出内存地址,用来存储变量score的值
pointer 是一个变量a pointer is a variable ,with the address of another variable as its value.多pointer变量的值是其他变量的内存地址,有了指针可以方便调用执行,做其他任务,比如dynamic memory allocation,就必须要有指针。the difference between pointers of different data types is the data type of the variable that the pointer points to.指针都享有同样的数据类型,a long hexadecimal number 表示一个内存地址。
Dynamic memory allocation is when an executing program requests that the operating system give it a block of main memory.动态内存分配就是执行程序时,操作系统应要求给予程序系统中的一块内存。 The program then uses this memory for some purpose.程序用这块儿内存做些事儿。 Usually the purpose is to add a node to a data structure.通常是给数据结构加一个节点 In object oriented languages, dynamic memory allocation is used to get the memory for a new object.在面向对象为导向的语言中,动态内存分配用于为新对象获得内存。
The memory comes from above the static part of the data segment. 这些内存来自数据分割间里静态部分Programs may request memory and may also return previously dynamically allocated memory. 程序要求内存也可以返还之前动态内存分配过的内存。Memory may be returned whenever it is no longer needed.内存在不需要的时候,就会返回总块 Memory can be returned in any order without any relation to the order in which it was allocated. 内存返回不需要与分配给内存时顺序一致,和之前分配的顺序没有关系,可以无序返回The heap may develop "holes" where previously allocated memory has been returned between blocks of memory still in use.堆可以仍在使用的内存块之间呈现出一些坑,这些坑是之前分配内存返回造成的,
A new dynamic request for memory might return a range of addresses out of one of the holes. But it might not use up all the hole, so further dynamic requests might be satisfied out of the original hole.
If too many small holes develop, memory is wasted because the total memory used by the holes may be large, but the holes cannot be used to satisfy dynamic requests. This situation is called memory fragmentation存储碎片. Keeping track of allocated and deallocated memory is complicated. A modern operating system does all this.这两段就是平时磁盘整理,有很碎片,这些碎片就是内存中分配之后有返回残留的一些碎片。这些碎片可以清理留下更多内存空间存放新的数据。
char *ptr;
if((ptr=(char *)malloc(0)) == NULL)
putc("Got a null pointer");
else
putc("Got a valid pointer");
}
这个片段输出的是什么,为什么
解析:这是一道动态内存分配(Dynamic memory allocation)题。
尽管不像非嵌入式计算那么常见,嵌入式系统还是有从堆(heap)中动态分配内存的过
程。
面试官期望应试者能解决内存碎片、碎片收集、变量的执行时间等问题。
这是一个有趣的问题。故意把0值传给了函数malloc,得到了一个合法的指针,这就是
上面的代码,该代码的输出是“Got a valid pointer”。我用这个来讨论这样的一道面试例题,
看看被面试者是否能想到库例程这样做是正确的。得到正确的答案固然重要,但解决问题的
方法和你做决定的基本原理更重要。
pointer像其他变量一样,必须先声明再使用,asterisk(*)用于声明函数,
int *ip;//pointer to an integer
double *dp;//pointer to a double
float *fp;//pointer to a float
char ch;//pointer to a character
我们给指针名字,定义类型,指针指向,“”的位置在数据类型之后,变量名或者中间都行。
using pointer:
分配一个变量地址给指针
int score = 5;
int *scorePtr;
scorePtr = &score;
cout<<scorePtr<<endl;//0x7fff527cc55c
现在scorePtr的值是score的存储地址
再例如:
int var = 5;
int ptr = &var;
11更多关于指针
operand操作数,Address-of operator (&)
返回存储于内存的变量值,Contents-of (or dereference)operator()
int var = 50;
int p;
p = &var;
cout<<var<<endl;//the value of var
cout<<p<<endl;//var's memory location--0x7fff52f4054c
cout<<p<<endl;//the value of the variable stored in the pointer p--50
int x = 5;
int p = &x;
x=x+4;
x=p+4;
p=p+4;//这三个赋值都相等。
int a =12; int b =13;
int *pa = &a;
int pb =&b;
pa+=pb;//same as a+=b
12静态和动态内存
理解动态内存如何工作非常重要。在C++中,内存分为两部分:栈stack和堆heap。
the stack:all of your local variables take up memory from the stack.
the heap: unused program memory that can be used when the program runs to dynamically allocate the memory.
每次在存贮信息的时候,你事先无法确定多少使用内存,以及定义的变量和内存的大小。这时,可以分配堆内的内存,使用new 操作符来返回分配的地址。
new int;
this allocates the memory size necessary for storing an integer on the heap, and returns that address.
动态内存,分配的地址可以存储于一个指针里,然后通过获得变量。
int *p = new int;
*p = 5;
the pointer p is stored in the stack as a local variable ,and holds the heap's allocated address as its value, the value of 5 is stored at that address in the heap
使用delete消除指针动态内存
for local variable on the stack, managing memory is carried out automatically.On the heap, it's necessary to manually handle the dynamically allocated memory, and use the delete operator to free up the memory when it's no longer needed.
delete pointer;//this statement releases the memory pointed to by pointer.
int *p = new int;//request memory
p = 5;//store value
cout<<p<<endl;//use value
delete p;//free up the memory
forgetting to free up memory that has been allocated with the new keyword will result in memory leaks, because that memory will stay allocated until the program shuts down.
dangling pointers悬挂指针
the delete operator free up the memory allocated for the variable, but does not delete the pointer itself, as the pointer is stored on the stack.
pointers that are left pointing to non-existent memory locations are called dangling pointers.
int*p = new int;//request memory
*p = 5;//store memory
delete p;//free up the memory
//now p is a dangling pointer
p = new int;//reuse for a new address
the NULL pointer is a constant with a value of zero that is defined in several of the standard libraries ,including iostream.
it's a good practice to assign NULL to a pointer variable when you declare it, in case you do not have exact address to be assigned.
a pointer assigned NULL is called a null pointer, for example;
intptr = NUll;
to allocate a memory for an int on the heap ,point to it with 'ptr', and then free the allocated memory
intptr = new int;
delete ptr;
new allocates space dynamically on the heap
delete de-allocates memory on the heap
12sizeof()操作符
to get a variable or data type's size, in bytes
cout<<"char:"<<sizeof(char)<<endl;
cout<<"int:"<<sizeof(int)<<endl;
cout<<"float:"<<sizeof(float)<<endl;
cout<<"double:"<<sizeof(double)<<endl;
int var = 50;
cout<<"var:"<<sizeof(var)<<endl;//var:4
size of an Array
double myArr[10];
cout<<sizeof(myArr)<<endl;// 80
double takes 8bytes then 80
int numbers[100];
cout<<sizeof(numbers)/sizeof[(numbers0]);//数组第一位来表示整个数组,sizeof(numbers)==sizeof(numbers【0】)
函数functions
1介绍
返回类型
int mian(){//最基本的一个函数main()函数
//some code
return 0;
}//返回值类型是int
void返回的是无值类函数,,它也是一个基本数据类型
定义函数
return_type function_name(parameter list)
{
body of the function
}
return_type:返回函数值的数据类型
function_name:函数名称
parameters:函数唤醒后,传递一个值给参数,作为施加的参数actual parameter 或者叫argument.
parameter list:指的是参数的类型,顺序和数量(int x)
body of the function:函数本体内容
void printSomething()
{
cout<<"Hi there!";
}//此函数名叫做printSomething返回值void没有参数
调用函数main()
int main()
{
printSomething();
return 0;
}
函数必须先声明才能调用,函数声明告诉编译器函数名称和如何调用函数。实际的函数主题可以分开定义:
include <iostream>
using namespace std;
//Function declaration
void printSomething();
int main()
{
printSomething();
return 0;
}
//Function definition
void printSomething()
{
cout<<"Hi there!";
}
2函数参数
对于一个函数使用自变量argument,它一定声明正式的参数formal parameters,自变量其实就是一个可以接受自变量值的变量x
void printSomething(int x)//parameter list:指的是参数的类型,顺序和数量(int x)
{
cout<<x;
}
正式参数的行为类似于本地其他的变量,进入函数的时候调用出函数的时候失效
include <iostream>
using namespace std;
//Function declaration
void printSomething(int x){
cout<<x;
}
int main()
{
printSomething(42);
}
//42作为一个自变量传递给函数,分配到正式参数x中,就算参数变化,函数的自变量x不会改变。
int timesTwo(int x){
return x2;
}
int main(){
cout<< timesTwo(5);
}
3多参数
int addNumbers(int x, int y){
//code goes here
}
每一个参数都需要声明数据类型和名称的
int addNumbers(int x, int y){
int result=x+y;
return result;
}
int main(){
cout<<addNumbers(45,42);
}
简化版:
int addNumbers(int x, int y){
return x+y;
}
int main(){
int result=addNumbers(43,24);
cout<<result<<endl;
return 0;
}
参数个数想多少就多少
4 返回函数recursive function calls itself
避免recursion 需要包含一个结束条件
如计算一个数的阶乘factorial
n!4!=4321=24
int factorial(int n){
if(n==1){
return 1;
}else{
return n*factorial(n-1);
}
}
int main(){
cout<<factorial(5);
}//作为base case对于返回函数非常必要,么有基本情况条件函数会一直跑下去。
5数组和函数
数组也可以作为参数当自变量
void printArray(int arr[], int size){
for(int x=0;x<size;x++){
cout<<arr[x]<<endl;
}
}
int main(){
int myArr[3]={34,23,55};
printArray(myArr,3);//调用printArray函数的时候,数组参数不需要用[],只有正式参数formal parameters才需要,一但自变量值赋予自变量,就直接代入名即可。
}
//
34
23
55
5函数自变量
通过值来传递自变量的实际值,代入informal parameter
通过reference间接方式传递。默认情况使用的是值传递。
void myFunc(int x){
x=100;
}
int main(){
int var=20;
myFunc(var);
cout<<var;
}//20
通过复制var,传递给函数myFunc,最初的自变量没有修改。函数内的参数没有改变不会影响自变量。
void myFunc(int *x){
*x=100;
}
int main(){
int var=20;
myFunc(&var);
cout<<var;
}//100相当于int *x=&var;cout<<var<<endl;var表示就是指针的值100
这种方式复制自变量的间接指针方式到正式参数中。函数内的指针调取时作为实际的自变量会受到影响。 a function taking an argument by reference can modify the eactual variable passed to it.
对象与类
对象导向的程序时接近真实世界的思考昂视,在编程世界里,对象是独立单元,每一个都有自己的身份,像真实世界那样,像苹果,马克杯等等,每一个都有独一无二的identity身份。有两个一模一样的马克杯,但是他们仍然是分开的两个独立个体。
有了身份identity就有特点characteristics比如马克杯是红色的,就是用来描述它的characteristic,或者叫做attributes属性。attribute描述的是目前对象的状态,state比如说empty,red,large都是马克杯的状态。
真实世界中,每个对象有behaves,马克杯静止的,汽车移动,电话响铃
所以对象的类型有三个维度表示,identity,attributes,behavior
对象时独立存在的,在计算机中,不像真实世界那样是以物质的形态出现,计算机中的对象可以是,银行账户,日期,时间等等。
因为他们都符合三要素:身份,属性,行为。
类class类作为对象的一个蓝本,总结描述出的一组特征类别。
就像建筑的图纸,用这个图纸可以不断建造一个个对象。
编程的工作原理就是类似于打造一个class,蓝本,然后建造出更多的对象。
每一个class有一个名字,描述属于自己的属性attribute和行为behavior
type是用于指一个类别名称,class name我们创建一个特别的对象类名
attributes也叫properties或者data,所以一个类有一个名字,还有描述对象的
属性和行为。
methods是另一个类的行为behavior,就是出于某个类的的函数。
methods类似于functions,都是可以被调用执行返回值的一组代码块,它是在类中声明的一个函数。
例如:
name:BankAccount
attributes:accountNumber,balance,dateOpened
behavior:open(),close(),deposit()类本身不具体到实际的数据,值提供definition
写完类后,我们可以以此创造对象,每一个对象都是类的一个例子instance,创造对象的过程叫做instantiation
声明一个类:
class BankAccount{
public://通过定义一个access specifier来区分类里的不同成员,public成员可以从类外获取,只要在类对象的范围内即可。
void sayHi(){
cout<<"Hi"<<endl;
}
};
下一步创造一个对象instantiate
int main(){
BankAccount test;//我们的对象名叫test有所有类定义的成员,注意(.)符号是 用来获取对象的方法函数的method of the object同函数一样,我们必须先声明一个类。
test.sayHi();
}
抽象
意思就是不用具体细节,我们关注与必要的特点,而不是具体的某一个特殊例子focus on the essential qualities of something。
比如书本,你不知道具体细节,比如页数,颜色,大小,但是你理解书的主旨,这就是the abstraction of the book
还有cout,你实际上用类oatream的cout对象,这个流数据是用来导出标准产出
抽象就是we can have a concept, but it's separate from any specific instance
抽象让我们写出单独的账户类,然后基于此创建每个账户,不同的对象
abstraction acts as a foundation for the other object orientation fundamentals, such as inheritance继承 and polymorphism多态
封装encapsulation或者叫data hiding,hide details of a class realizaiton
封装的意思是围绕一个实体,不让里面的出来,同时保护。在对象导向中,封装就是在类内属性与行为的简单组合和限制获取类内访问途径的作用。
例如在上面BankAccount类中,我们不想让人未经验证就进入存取行为,deposit()\withdraw()所以我们控制获取权限影藏部分属性,要求必须通过black boxing,才能达到内部非公开信息的部分。所以封装的意义在于1封装对象的属性和方法(methods)2隐藏对象的一切,只展示必要的一部分。
access specifier访问说明符;访问指示符;存取说明符
用于访问类特殊成员的获取级别
三类访问层级:public,protected,private
public成员从类外壳获取,只要是类对象的范围都可以获取
例如
include<iostream>
include<string>
using namespace std;
class myClass{
public:
string name;
};
int main(){
myClass myObj;
myObj.name="SoloLearn";
cout<<myObj.name;
return 0;
}
//SoloLearn
name的属性是public的,可以从外部代码中获得。获取modifiers修饰语只要被声明一次就可以了,多成员可以跟用一个单独的访问说明public:
private成员从类外部不能被访问,查看,只能从类内部。public成员的函数可以访问private成员。
include<iostream>
include<string>
suing namespace std;
class myClass{
public:
void setName(string x){
name=x;
}
private:
string name;
};
int main(){
myClass myObj;
myObj.setName("John");
return 0;
}//默认没有访问说明,所有类成员都是private的
封装的一个例子:
class myClass{
public:
void setName(string x){ //setName()方法用于设置name属性
name=x;
}
string getName(){//添加另一个public成员获取属性值
return name:
}
private:
string name;
};//the getName() method returns the value of the private name attribute.
class MyClass
{
private:
int myData;
public:
int getMyDate(){
return myData;
}
};//getMyData() method returns the value of the private name attribute.
揉在一起,访问说明符:
include<iostream>
include<string>
using namespace std;
class myClass{
public:
void setName(string x){
neme=x
}
string getName(){
return name;
}
private:
string name;
};
int main(){
myClass myObj;
myObj.setName("John");
cout<<myObj.getName();
return 0;
}//John;我们用封装隐藏name属性,使用public方法提供访问权限。我们类数据可以读取修改仅通过这些方法。
类constructors是特殊成员函数,当新建一个类对象的时候就被执行此操作。
constructor的名字与类是一直的,没有返回类型,return type,甚至没有void
class myClass{
public:
myClass(){
cout<<"Hey";
}//upon the creation of an object of type myClass,the constructor is automatically called;
void setName(string x){
return name;
}
private:
string name;
};
int main(){
myClass myObj;
return 0;
}//Hey
constructors can be very useful for setting initial values of certain member variables. a default constructor has no parameters.however,when needed,parameters can be added to a constructor.this make s it possible to assign an initial value to an object when it's created, as shown in the following example:
class myClass{
public:
myClass(string nm){
setName(nm);
}
void setName(string x){
name=x;
}
string getName(){
return name;
}
private:
string name;
};
另一个例子:
class my_class
{
private:
int my_data;
public:
my_class(int arg){
my_data=arg;
}
};
可以有多个constructors取不同的参数值
class myClass{
public:
myClass(string nm){
setName(nm);
}
void setName(string x){
name=x;
}
string getName(){
return name;
}
private:
string name;
};
int main(){
myClass obj1("David");
myClass obj2("Amy");
cout<<obj.getName();
}//David
class MyClass{
private:
int myData;
public:
MyClass(int arg){
myData=arg;
}
};
int main(){
MyClass obj(14);
}
an object is an instance of a class
identity,attribute,behavior are three terms used to describe an object-oriented programming
a class can have as many as you want functions
the public and private keywords are called access specifiers
class student
{
public:
void hello(){
cout<<"hi from student"<<endl;
}
};
clss student{
private:
int age:
public:
Student(int a){
setAge();
}
void setAge(int a){
age=a;
}
}
header and source these two files are usually defined for a class
header file(.h) holds the function declarations(prototypes)和variable declarations
it currently includes a template for our new MyClass class,with one default constructor.(MyClass.h)
ifndef MYCLASS_H
define MYCLASS_H
class MyClass
{
public:
MyClass();
protected:
private:
}
endif//MYCLASS_H
the implementation of the class and its methods go into the source file (.cpp).
currently it includes just an empty constructor.
MyClass.cpp
include"MyCLass.h"
MyClass::MyClass()
{
//ctor
}
the #ifdef and #define statements in the header file will be discussed later
scope resolution operator
双冒号,double colon在源文件(.cpp)中叫做scope resolution operator
include“MyCLass.h”
MyClass::MyClass()
{
//ctor
}//scope resolution operator用于敌营类成员函数,在源文件中我们定义constructor prototype,so MyClass::MyClass() 指的是MyClass成员函数,这里就是MyClass类的constructor
使用新建的MyClass在main函数中需要添加header 文件
include<iostream>
include "MyClass.h"
int main(){
MyClass obj;
}//header 声明类做什么,cpp source决定怎样执行那些特征
使用类,需要在main.cpp中包含header file
删除类:
destructors 对应的是constructors,constructor runs when an object is created,destructor runs when it is deleted
class MyClass{
public:
~MyClass(){
//some code
}
};
class MyClass{
public:
MyClass()
~MyClass();
};
以上在header file中
下面是cpp source 中:
include"MyCLass.h"
include<iostream>
using namespace std;
MyClass::MyClass()
{
cout<<"Constructor"<<endl:
}
MyClass::~MyClass()
{
cout<<"Destructor"<<endl:
}//因为我们包含了<iostream>header,所以能用cout
destructors没有参数,所以不能重载,每一个类仅有一个destructor,只有在需要删除对象的时候才用。