2021-04-28 17:49:18 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 AIIT XUOS Lab
|
|
|
|
* XiUOS is licensed under Mulan PSL v2.
|
|
|
|
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
|
|
|
* You may obtain a copy of Mulan PSL v2 at:
|
|
|
|
* http://license.coscl.org.cn/MulanPSL2
|
|
|
|
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
|
|
|
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
|
|
|
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the Mulan PSL v2 for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <xiuos.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <cstdlib>
|
|
|
|
using namespace std;
|
|
|
|
extern "C" void KPrintf(const char *fmt, ...);
|
|
|
|
|
|
|
|
class Animal //parent class
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2021-04-29 12:55:01 +08:00
|
|
|
virtual void Eat()
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
KPrintf("eat\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:55:01 +08:00
|
|
|
void Sleep()
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
|
|
|
KPrintf("sleep\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
class Fish :public Animal //subclass
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2021-04-29 12:55:01 +08:00
|
|
|
void Eat()
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
|
|
|
KPrintf("fish eat\n");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-04-29 12:55:01 +08:00
|
|
|
void Doeat(Animal& animal)
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
2021-04-29 12:55:01 +08:00
|
|
|
animal.Eat();
|
2021-04-28 17:49:18 +08:00
|
|
|
}
|
|
|
|
|
2021-04-29 12:55:01 +08:00
|
|
|
void MemTest2()
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *ptr = NULL; /* memory pointer */
|
|
|
|
|
2021-04-29 12:55:01 +08:00
|
|
|
for (i = 0; ; i++) {
|
2021-04-28 17:49:18 +08:00
|
|
|
/* allocate (1<<i) bytes memory every single time */
|
|
|
|
ptr = (char *)operator new(1 << i);
|
|
|
|
|
|
|
|
/* if allocate successfully */
|
2021-04-29 12:55:01 +08:00
|
|
|
if (ptr != NULL) {
|
2021-04-28 17:49:18 +08:00
|
|
|
KPrintf("get memory :%d byte\n", (1 << i));
|
|
|
|
/* release the memory */
|
|
|
|
operator delete(ptr);
|
|
|
|
KPrintf("free memory :%d byte\n", (1 << i));
|
|
|
|
ptr = NULL;
|
2021-04-29 12:55:01 +08:00
|
|
|
} else {
|
2021-04-28 17:49:18 +08:00
|
|
|
KPrintf("try to get %d byte memory failed!\n", (1 << i));
|
|
|
|
break;
|
|
|
|
//return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-29 12:55:01 +08:00
|
|
|
void OverLoadTest(int a)
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
|
|
|
KPrintf("output is a int number: %d\n", a);
|
|
|
|
}
|
2021-04-29 12:55:01 +08:00
|
|
|
|
|
|
|
void OverLoadTestDouble(int a,int b )
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
|
|
|
KPrintf("output is 2 int number: %d and %d\n", a,b);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2021-04-29 12:55:01 +08:00
|
|
|
void MySwap(T& a, T& b)
|
2021-04-28 17:49:18 +08:00
|
|
|
{
|
|
|
|
T temp = a;
|
|
|
|
a = b;
|
|
|
|
b = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" int cppmain(void)
|
|
|
|
{
|
2021-04-29 12:55:01 +08:00
|
|
|
MemTest2();
|
2021-04-28 17:49:18 +08:00
|
|
|
|
|
|
|
class Fish fish;
|
2021-04-29 12:55:01 +08:00
|
|
|
Doeat(fish);
|
2021-04-28 17:49:18 +08:00
|
|
|
|
|
|
|
int a = 3;
|
|
|
|
int b = 5;
|
2021-04-29 12:55:01 +08:00
|
|
|
void OverLoadTestDouble(int a, int b);
|
|
|
|
OverLoadTestDouble(a, b);
|
|
|
|
MySwap(a,b);
|
2021-04-28 17:49:18 +08:00
|
|
|
KPrintf("with template the output is: %d and %d\n", a,b);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|