Data Type
int aint;
float afloat;
long long alonglong;
double adouble;
typedef struct{
int num;
char name[20];
float fsocre;
}student;
student stu1, stu[31];
Pointer
A pointer holds the address of the other objects, and we can directly access the physical memory address of the pointed object by using pointer.
int var_run=10;
int *p;
p=&var_run;
datatype of pointer p
must align with var_run
.
Function Overloading
#include<stdlib.h>
#include<iostream>
using namespace std;
void fun(int i=30,int x = 10, int j = 20);
void fun(double i, double j);
int main() { //Default value from function
//fun();
fun(100);
fun(100,200);
fun(100, 200, 300); //Function overloadingfun(1.1, 1.2);
return 0;
}
void fun(int i, int j, int k) {
cout << i << "," << j << "," << k << endl;
}
void fun(double i, double j) {
cout << i << "," << j << endl;
}
OO
Features of Object-oriented
- Encapsulation: the use of class 封装性
class contains data and method.
-
Inheritance: super class & sub-class reuse code 继承性
-
Polymorphism: reuse interface. 多态性
same interface, different methods.
class A{
void f1() {printf(“1\n”); }
virtual void f2() {printf(“2\n”);
};
class B : public A
{
void f1() { printf(“3\n”); }
void f2() { printf(“4\n”); }
};
int main(){
A a; B b;
A *p=&a;
p->f1(); //1
p->f2(); //2
p=&b;
p->f1(); //1
p->f2(); //4
}
Constructor
Used to initialize the class so that we can give the data of the object an initial value.
- The name of constructor is the same as the class.
- have it's own parameters just like normal functions.
- It's auto executed during the creation of objects.
PO VS OO
procedure-oriented versus object-oriented
The most difference is that while procedural programming uses procedures to operate data on data structures, object-oriented programming bundles the two together, called an "object", which is an instance of a class, operates on its "own" data structure.