C++学习心得(本文是我在拥有一定c语言、java入门基础上,花费四天学习c++的一些笔记,由于健忘,故作此可以在日后对于一些容易忘记的内容可以及时查看,复习c++的内容):
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//7-16
/void bubbleSort(int arr, int len)
{
for(int i=0;i<len-1;i++)
for (int j = 0; j < len - i - 1; j++)
{
if (arr[j] > arr[j + 1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
struct Student
{
string name;
int age;
int score;
};
void out(Student stu)
{
cout << stu.name <<" “<< stu.age <<” "<< stu.score << endl;
}
void out1(const struct Student * p)
{
//p->score=99;
cout << p->name << " " << p->age << " " << p->score << endl;
}
int main()
{
srand((unsigned int)time(NULL));
int e = rand() % 60 + 40;
int arr[] = { 3,7,1,5,4,9,2,8,6,0 };
int len = sizeof(arr) / sizeof(arr[0]);
for (int n = 0; n < len; n++)
{
cout << arr[n] << " ";
}
cout << endl;
bubbleSort(arr, len);
for (int k = 0; k < len; k++)
{
cout << arr[k]<<" ";
}
cout << endl;
struct Student s1;
s1.name = "xiaoming";
s1.age = 12;
s1.score = 98;
struct Student s2 = { "xiaoqiang",14,88 };
struct Student stuArr[2] = {
{"xiaomi",20,76},
{"xiaohua",17,80}
};
Student * l = &s2;
cout << l->age << endl;
out(s2);
out1(&s1)
system("pause");
return 0;
}
*/
//7-17
/int func()//栈区
{
int a = 10;
return &a;
}
intfunc1()//堆区
{
intw = new int(10);
return w;
}
void test()
{
int*arr = new int[10];
delete[]arr;
}
void mySwap(int &a, int &b) //引用传递
{
int temp = a;
a = b;
b = temp;
}
int & test1()
{
int a = 10;
return a;
}
int & test2()
{
static int a = 10;
return a;
}
//封装
const double PI = 3.14;
class Circle
{
public:
int r;
double calculate()
{
return 2 * PI*r;
}
};
class Student
{
public:
string name;
int id;
void showStudent()
{
cout << name << " " << id << endl;
}
void setName(string name)
{
this->name = name;
}
void setId(int id)
{
this->id = id;
}
};
//多态
class Animal
{
public:
virtual void speak()
{
cout << “动物在说话” << endl;
}
};
class Cat :public Animal
{
public:
virtual void speak()
{
cout << “小猫在说话” << endl;
}
};
class Dog :public Animal
{
public:
virtual void speak()
{
cout << “小狗在说话” << endl;
}
};
void doSpeak(Animal &animal)
{
animal.speak();
}
void test1()
{
Cat cat;
doSpeak(cat);
Dog dog;
doSpeak(dog);
}
//计算器类
class AbstractCalculator
{
public:
virtual int getResult()//纯虚函数 virtual void func()=0
{
return 0;
}
int numA;
int numB;
};
class mulCalculator :public AbstractCalculator
{
public:
int getResult()
{
return numA * numB;
}
};
void test3()
{
AbstractCalculator *abc = new