OOP.cpp

 
/*
This is My First Program in My OOP Class
this Program is for very very beginner that want to
learn more about Inheritance, Incapsulation or
Polymorphism in C++.I hope You can't Learn OOP in this simple program
it will run properly in Visual C++
sorry about the varibel, it's still in indonesian.
Special Thanx for my Class Asst Efra
who give me this assignment.
Press
A : for Point
S : for line
D : for Box
I : move up
J : move left
K : move right
L : move Right
Q : quit
*/

#include<iostream.h>
#include<windows.h>
#include<conio.h>

HANDLE hcon;
COORD pos;

void clrscr()
{
for (int i = 0; i <25; i++)
{
cout << endl;
cout.flush();
}
pos.X=0;
pos.Y=0;
SetConsoleCursorPosition(hcon,pos);
}

class ttitik
{
protected :
char ttk;
COORD posisi;
public :
void move(int dx,int dy);
virtual void hide();
virtual void cetak();
ttitik();
};

ttitik::ttitik()
{
posisi.X = 0;
posisi.Y = 0;
}

void ttitik::cetak()
{
SetConsoleCursorPosition(hcon,posisi);
cout <<'*';
cout.flush();
}

void ttitik::hide()
{
SetConsoleCursorPosition(hcon,posisi);
cout <<' ';
cout.flush();
}
void ttitik::move(int posx,int posy)
{
//clrscr();
hide();
posisi.X +=posx;
posisi.Y +=posy;
cetak();
}

class tgaris: public ttitik 

protected:
int pjg;
public:
    virtual void cetak();
virtual void hide();
    tgaris();
};

tgaris::tgaris()
{
pjg=5;
}


void tgaris::cetak()
{
SetConsoleCursorPosition(hcon,posisi);
for ( int i=0;i<pjg;i++)
{
cout <<'*';
  
}     
cout.flush();
}   

void tgaris::hide()
{
SetConsoleCursorPosition(hcon,posisi);
for ( int i=0;i<pjg;i++)
{
cout <<' ';
  
}     
cout.flush();
}   


class tkotak: public tgaris 

protected:
int lbr;
public:
    virtual void cetak();
virtual void hide();
    tkotak();
};

tkotak::tkotak()
{
lbr=5;
}
void tkotak::cetak()
{
COORD temp;
for (int i=0;i<lbr;i++) 
{
for (int j=0;j<pjg;j++) 
{
    temp.X=posisi.X+j;
temp.Y=posisi.Y+i;
SetConsoleCursorPosition(hcon,temp);
cout<<"*";
cout.flush();

}
}



}

void tkotak::hide()
{
COORD temp;
for (int i=0;i<lbr;i++) 
{

for (int j=0;j<pjg;j++) 
{
    temp.X=posisi.X+j;
temp.Y=posisi.Y+i;
SetConsoleCursorPosition(hcon,temp);
cout<<" ";
cout.flush();

}
}
}



void main()
{

ttitik titik;
tgaris garis;
tkotak kotak;
char tekan;
char type;
hcon = GetStdHandle(STD_OUTPUT_HANDLE);
tekan = getch();

while (tekan != 'q')
{
switch (tekan)
{
case 'j' :  switch(type)
{
case 1: titik.move(-1,0);break;
case 2: garis.move(-1,0);break;
case 3: kotak.move(-1,0);break;
}
break;
case 'l' : switch(type)
{
  case 1:titik.move(1,0);break;
case 2:garis.move(1,0);break;
case 3:kotak.move(1,0);break;
}
break;
case 'i' : switch(type)
{
case 1:titik.move(0,-1);break;
case 2:garis.move(0,-1);break;
case 3:kotak.move(0,-1);break;
}
break;
case 'k' : switch(type)
{
  case 1:titik.move(0,1);break;
case 2:garis.move(0,1);break;
case 3:kotak.move(0,1);break;
}
break;

case 'a':clrscr();type=1;break;
case 's':clrscr();type=2;break;
case 'd':clrscr();type=3;break;

}
tekan = getch();
}

}




Project Homepage: