Skip to content

Instantly share code, notes, and snippets.

@Gumball12
Last active November 21, 2019 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gumball12/5081eb311414c6d59960df75ed18955d to your computer and use it in GitHub Desktop.
Save Gumball12/5081eb311414c6d59960df75ed18955d to your computer and use it in GitHub Desktop.
19-2h tutoring https://git.io/Je4r2
// https://rextester.com/KAY13591
// import modules
#include <iostream>
// define Character class
class Character {
private:
// declare fields
int x;
int hp;
public:
// constructor
Character(int initX, int initHp): x(initX), hp(initHp) {
// print message
std::cout << "캐릭터 생성" << std::endl;
}
// print character's status
void print() {
std::cout << "상태 출력 => x: "
<< x
<< ", HP: "
<< hp << std::endl;
}
// 'x' setter
void setX(int newX) {
std::cout << "캐릭터 이동" << std::endl;
x = newX; // set new x
}
};
// entry point function
int main(void) {
Character c { 0, 100 };
c.print();
c.setX(90);
c.print();
return 0;
}
// https://rextester.com/RPWF16481
// import modules
#include <iostream>
#include <vector>
// using namespace for 'string' type
using namespace std;
// define Todo Class
class Todo {
private:
vector<string> todos; // todo array
public:
// Constructor
Todo() {
// creation message
std::cout << "Created todo class" << std::endl;
}
// add todo into the 'todos' list
int add(string todo) {
todos.push_back(todo); // push_back
return todos.size(); // return this size
}
// print todos
void print() {
for (string &t : todos) { // for-eaching
std::cout << t << std::endl; // print todo
}
}
};
// entry point function
int main(void) {
// create Todo instance
Todo todo;
// append todos
todo.add("item 1");
todo.add("item 2");
todo.add("item 3");
// print
todo.print();
return 0;
}
// https://rextester.com/HLH22324
// import module
#include <iostream>
// define Apple class
class Apple {
public:
// print method
void print() {
// print message
std::cout << "Apple" << std::endl;
}
};
// define People class
class People {
private:
Apple * apple; // apple field variable (using pointer type)
public:
// constructor
// pointer type var => to point to a Heap instance (Apple instance)
People(Apple * a) { // shallow copy
apple = a;
}
// eat function
void eat() {
// call print method
apple->print(); // or `(*apple).print();`
}
};
// entry point
int main()
{
// create instances
Apple * a = new Apple();
People * p = new People(a);
// call eat
p->eat(); // or `(*p).eat();`
return 0;
}
// https://rextester.com/BROY71588
#include <iostream>
using namespace std;
class Ticket {
private:
static int cnt;
public:
Ticket() {
cnt++; // Ticket::cnt++;
}
static int count() {
return cnt; // Ticket::cnt;
}
};
int Ticket::cnt = 0;
int main()
{
Ticket t1;
Ticket t2;
Ticket t3;
cout << Ticket::count() << endl;
}
// https://rextester.com/LECWL51773
//g++ 5.4.0
#include <iostream>
class B; // declaration class
class A {
private:
int numberA;
public:
A(): numberA(100) { }
friend int add(A, B);
};
class B{
private:
int numberB;
public:
B(): numberB(200) { }
friend int add(A, B);
};
int add(A a, B b) {
return a.numberA + b.numberB;
}
int main()
{
A a;
B b;
std::cout << add(a, b);
return 0;
}
// https://rextester.com/ISUB26014
//g++ 5.4.0
#include <iostream>
class Counter {
private:
int value;
public:
Counter(): value(0) { }
int operator ++ () {
value += 2;
std::cout << value << std::endl;
return value;
}
};
int main()
{
Counter c;
++c;
++c;
++c;
return 0;
}
//g++ 5.4.0
#include <iostream>
class Friend {
private:
int friendValue;
friend class Father;
public:
Friend(): friendValue(100) { }
};
class Father {
protected:
int getFriendValue(Friend f) {
return f.friendValue;
}
};
class Son: public Father {
public:
int getValue(Friend f) {
return getFriendValue(f);
}
};
int main()
{
Friend f;
Son s;
std::cout << s.getValue(f) << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment