toaruos/userspace/cpp_test.cpp

77 lines
1.4 KiB
C++
Raw Normal View History

2012-03-21 01:30:59 +04:00
/* vim: tabstop=4 shiftwidth=4 noexpandtab
*
* C++ Test Program
*/
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <string>
#include <fstream>
#include <iostream>
2012-03-21 01:30:59 +04:00
/* A class */
class test {
private:
2012-03-21 01:30:59 +04:00
/* With a little private integer */
int i;
public:
test() {
2012-03-21 01:30:59 +04:00
/* That starts at zero */
i = 0;
}
void inc() {
2012-03-21 01:30:59 +04:00
/* And can be incremented */
i++;
}
int value() {
2012-03-21 01:37:49 +04:00
/* And can have its value retrieved */
return i;
}
};
int main() {
2012-03-21 01:30:59 +04:00
/*
* HACK: We need to execute ios_base::Init() to ensure the base
* IO objects are accessible (this would normally be called by
* the static constructor for std::__ioinit, but we don't currently
2012-03-21 01:37:49 +04:00
* execute static constructors as we never execute the .init section)
2012-03-21 01:30:59 +04:00
*/
std::ios_base::Init();
2012-03-21 01:30:59 +04:00
/*
* First test: Print "Hello world" and a new line.
*/
std::cout << "Hello world\n";
2012-03-21 01:30:59 +04:00
/* Create a test object */
test * t = new test();
2012-03-21 01:30:59 +04:00
/* Print its value = 0 */
printf("%d\n", t->value());
2012-03-21 01:30:59 +04:00
/* Increment it */
t->inc();
2012-03-21 01:30:59 +04:00
/* And print again = 1 */
printf("%d\n", t->value());
2012-03-21 01:30:59 +04:00
/* Create a queue */
std::queue<int> q;
q.push(4);
q.push(20);
q.push(5);
q.push(19);
2012-03-21 01:30:59 +04:00
/* Print out the values in order */
while (!q.empty()) {
printf("%d\n", q.front());
q.pop();
}
2012-03-21 01:30:59 +04:00
/* Read in a string */
std::string s;
std::cin >> s;
2012-03-21 01:30:59 +04:00
/* Print it and its length */
printf("%d: %s\n", s.length(), s.c_str());
return 0;
}