Hanya Coretan Anak Bangsa

Minggu, 08 Juli 2012

babV | absract


babV | absract
Kelas abstract adalah kelas murni yang tidak boleh memiliki objek, dan satu/lebih method-methodnya yang abstract harus diimplementasikan (override) oleh kelas turunannya.

contoh programnya menggunakan C# :




/*
* Created by SharpDevelop.
* User: dino fajar rizkian
* Date: 08/07/2012
* Time: 14:05
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;

namespace abstrak
{
abstract public class gamers {
public string namachar,idchar,server;
public void getdata()
{
Console.Write("masukan namachar : ");
namachar = Console.ReadLine();
Console.Write("masukan idchar : ");
idchar = Console.ReadLine();
Console.Write(" Server : ");
server = Console.ReadLine();
}
public abstract void taruhdata();
}
class user1 :gamers {
public override void taruhdata(){
Console.WriteLine("Server : "+ server);
}
}
class user2 : gamers{
public override void taruhdata(){
Console.WriteLine("Server b: "+ server);
}
}
class Program
{
public static void Main(string[] args)
{
user1 u1 = new user1();
user2 u2 = new user2();
Console.WriteLine();
Console.WriteLine("Masukan data gamers 1 ");
u1.getdata();
Console.WriteLine("Masukan data gamers 2 ");
u2.getdata();
Console.WriteLine("\ndata gamers 1");
u1.taruhdata();
Console.WriteLine("\ndata gamers 2");
u2.taruhdata();
Console.ReadKey(true);
}
}
}
Hasilnya seperti dibawah ini:



Kalo ini menggunakan Java:


package abstrak;

import java.util.*;

abstract class gamers{
public Scanner input = new Scanner(System.in);
public String namachar;
long idchar;
public String server;
public void getdata(){
System.out.print("masukan namachar : ");
namachar = input.next();
System.out.print("masukan idchar : ");
idchar = input.nextLong();
System.out.print("masukan server : ");
server = input.next();
}
public abstract void taruhdata();
}
class user1 extends gamers{
public @Override void taruhdata(){
System.out.println(" namachar : " +namachar);
System.out.println(" idchar : " +idchar);
System.out.println(" Server a : " +server);

}
}
class user2 extends gamers{

public @Override void taruhdata(){
System.out.println(" namachar : " +namachar);
System.out.println(" idchar : " +idchar);
System.out.println(" Server b : " +server);

}
}
public class Main {
public static void main(String[] args) {
user1 u1 = new user1();
user2 u2 = new user2();
System.out.println();
System.out.println("masukan data gamers1");
u1.getdata();
System.out.println("masukan data gamers2");
u2.getdata();
System.out.println("\ndata gamers1");
u1.taruhdata();
System.out.println("\ndata gamers2");
u2.taruhdata();
}

}
Hasilnya seperti ini:



  
Kalo yang ini menggunakan C++ :
#include <iostream>

const int limit = 50;
using namespace std;

class gamers {
private:
char namachar [limit];
unsigned long idchar;
public:
void getdata(){
cout << "masukan namachar   : ";
cin >> namachar;
cout << "masukan idchar : ";
cin >> idchar;
}
void taruhdata(){
cout << " nachar    : " << namachar << endl;
cout << " Idchar    : " << idchar ;
}
};
class user1 : public gamers {
private :
char server[limit];
public:
void getdata(){
gamers::getdata();
cout << "Server : ";
cin >> server;

}
void taruhdata(){
gamers::taruhdata();
cout << endl << "Server :"<<server;
}
};
class user2:public gamers{
};
int main()
{
    user1 u1;
    user2 u2;
    cout << endl;
    cout << "masukan data gamers1" << endl;
    u1.getdata();
    cout << "masukan data gamers2" << endl;
    u2.getdata();
    cout << "\ndata gamers1" << endl;
    u1.taruhdata();
    cout << "\ndata gamers2" << endl;
    u2.taruhdata();
    return 0;
}

Hasilnya seperti ini:










0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Web Host