Created
November 3, 2020 18:29
-
-
Save abdullahainun/724ea0e419043cbd711ecf5bc7c9b0c0 to your computer and use it in GitHub Desktop.
cpp sample oop project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
using namespace std; | |
// Base Class | |
class Kendaraan { | |
private: | |
// Private attribute | |
string nama; | |
int jumlahRoda; | |
int jumlahKursi; | |
int posisi; | |
public: | |
Kendaraan() { // Constructor | |
nama = ""; | |
jumlahRoda = 0; | |
jumlahKursi = 0; | |
posisi = 0; | |
} | |
Kendaraan(string nama, int jumlahRoda, int jumlahKursi, int posisi) { // Constructor | |
nama = nama; | |
jumlahRoda = jumlahRoda; | |
jumlahKursi = jumlahKursi; | |
posisi = posisi; | |
} | |
// Setter nama | |
void setNama(string param) { | |
nama = param; | |
} | |
// Getter nama | |
string getNama() { | |
return nama; | |
} | |
// Setter jumlahRoda | |
void setJumlahRoda(int param) { | |
jumlahRoda = param; | |
} | |
// Getter jumlahRoda | |
int getJumlahRoda() { | |
return jumlahRoda; | |
} | |
// Setter jumlahKursi | |
void setJumlahKursi(int param) { | |
jumlahKursi = param; | |
} | |
// Getter jumlahKursi | |
int getJumlahKursi() { | |
return jumlahKursi; | |
} | |
// Setter posisi | |
void setPosisi(int param) { | |
posisi = param; | |
} | |
// Getter posisi | |
int getPosisi() { | |
return posisi; | |
} | |
}; | |
// Derived class | |
class Mobil: public Kendaraan { | |
private: | |
string warna = ""; | |
public: | |
// Constructor | |
Mobil(string nama, int jumlahRoda, int jumlahKursi, int posisi, string warna):Kendaraan(nama, jumlahRoda, jumlahKursi, posisi) | |
{ | |
if( jumlahRoda <= 4 && jumlahKursi <= 4) | |
{ | |
setNama(nama); | |
setJumlahRoda(jumlahRoda); | |
setJumlahKursi(jumlahKursi); | |
setPosisi(posisi); | |
setWarna(warna); | |
}else{ | |
Kendaraan(); | |
cout << "Maaf parameter mobil yang anda inputkan tidak valid \n"; | |
} | |
} | |
// Setter warna | |
void setWarna(string param) { | |
warna = param; | |
} | |
// Getter warna | |
string getWarna() { | |
return warna; | |
} | |
void profil() { | |
cout << "Spesifikasi Mobile: \n"; | |
cout << "Nama = " << getNama() << "\n"; | |
cout << "jumlahRoda = " << getJumlahRoda() << "\n"; | |
cout << "jumlahKursi = " << getJumlahKursi() << "\n"; | |
cout << "posisi = " << getPosisi() << "\n"; | |
cout << "warna = " << getWarna() << "\n"; | |
} | |
int drive(int dist) { | |
setPosisi(dist); | |
return getPosisi(); | |
} | |
}; | |
// Derived class | |
class SepedaMotor: public Kendaraan { | |
public: | |
// Constructor | |
SepedaMotor(string nama, int jumlahRoda, int jumlahKursi, int posisi):Kendaraan(nama, jumlahRoda, jumlahKursi, posisi) | |
{ | |
if( jumlahRoda <= 2 && jumlahKursi == 1 || jumlahKursi == 2 && posisi >= 0) | |
{ | |
setNama(nama); | |
setJumlahRoda(jumlahRoda); | |
setJumlahKursi(jumlahKursi); | |
setPosisi(posisi); | |
}else{ | |
Kendaraan(); | |
cout << "Maaf parameter sepeda motor yang anda inputkan tidak valid \n"; | |
} | |
} | |
void profil() { | |
cout << "Spesifikasi Sepeda Motor:\n"; | |
cout << "Nama = " << getNama() << "\n"; | |
cout << "jumlahRoda = " << getJumlahRoda() << "\n"; | |
cout << "jumlahKursi = " << getJumlahKursi() << "\n"; | |
cout << "posisi = " << getPosisi() << "\n"; | |
} | |
int drive(int dist) { | |
if(dist >= 0 ) | |
{ | |
setPosisi(dist); | |
return getPosisi(); | |
}else{ | |
cout << "Maaf parameter funsi drive sepeda motor tidak boleh kurang dari 0 \n"; | |
setPosisi(0); | |
return getPosisi(); | |
} | |
} | |
}; | |
int main() { | |
Mobil mobil("avanza", 4, 4, 0, "hitam"); | |
SepedaMotor sepedamotor("nmax", 2, 2, 0); | |
mobil.drive(-5); | |
mobil.profil(); | |
cout << "\n"; | |
sepedamotor.drive(-1); | |
sepedamotor.profil(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
┌─[latifa][]
└─▪ g++ -o source source.cpp
┌─[latifa][]
└─▪ ./source
Spesifikasi Mobile:
Nama = avanza
jumlahRoda = 4
jumlahKursi = 4
posisi = -5
warna = hitam
Maaf parameter funsi drive sepeda motor tidak boleh kurang dari 0
Spesifikasi Sepeda Motor:
Nama = nmax
jumlahRoda = 2
jumlahKursi = 2
posisi = 0
┌─[latifa][]
└─▪