Csharp
using System; namespace NestedClass { class Program { public static void Main(string[] args) { satu s = new satu(); satu.dua sd = new NestedClass.satu.dua(); //persegi Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Menghitung Luas Persegi"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); Console.Write("Masukkan sisi persegi = "); s.A = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Luas Persegi = {0}",s.Persegi()); Console.WriteLine("\n\n"); //persegi panjang Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Menghitung Luas Persegi Panjang"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(); Console.Write("Masukkan Panjang = "); sd.A = Convert.ToInt32(Console.ReadLine()); Console.Write("Masukkan Lebar = "); sd.B = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Luas Persegi Panjang = {0}",sd.PersegiPanjang()); Console.Write("\n\nPress any key to continue . . . "); Console.ReadKey(true); } } class satu { protected int a; protected int b; public int A { get { return a; } set { a = value; } } public int B { get { return b; } set { b = value; } } public int Persegi() { return A*4; } public class dua : satu { public int PersegiPanjang() { return A*B; } } } }
Java
package NestedClass; import NestedClass.Satu.Dua; import java.util.Scanner; public class MainClass { private static Scanner baca = new Scanner(System.in); public static void main(String[] args) { Satu s = new Satu(); Satu.Dua sd = s.new Dua(); int a,b; //persegi System.out.println("Menghitung Luas Persegi"); System.out.println(); System.out.print("Masukkan sisi persegi = "); a = baca.nextInt(); s.setA(a); System.out.println("Luas Persegi = "+s.Persegi()); System.out.println("\n\n"); //persegi panjang System.out.println("Menghitung Luas Persegi Panjang"); System.out.println(); System.out.print("Masukkan Panjang = "); a = baca.nextInt(); System.out.print("Masukkan Lebar = "); b = baca.nextInt(); sd.setA(a); sd.setB(b); System.out.println("Luas Persegi Panjang = "+sd.PersegiPanjang()); System.out.print("\n\nPress any key to continue . . . "); } } class Satu { protected int a,b; public void setA(int x) { this.a = x; } public int GetA() { return a; } public void setB(int y) { this.b = y; } public int GetB() { return b; } public int Persegi() { return GetA()*4; } class Dua extends Satu { public int PersegiPanjang() { return GetA()*GetB(); } } }
C++
#include <cstdlib> #include <iostream> using namespace std; class Satu { protected : int a,b; public : void setA(int x) { a = x; //cout<<a; } int GetA() { return a; } void setB(int y) { b = y; } int GetB() { return b; } int Persegi() { return GetA()*4; } class Dua; }; class Satu::Dua : public Satu { public : void Show() { cout<<"Halo"<<endl; } public: int PersegiPanjang() { return GetA()*GetB(); } }; int main(int argc, char *argv[]) { Satu s; Satu::Dua sd; int a,b; //persegi cout<<"Menghitung Luas Persegi"<<endl<<endl; cout<<"Masukkan sisi persegi = "; cin>>a; s.setA(a); cout<<"Luas Persegi = "<<s.Persegi()<<endl; cout<<endl<<endl<<endl; //persegi panjang cout<<"Menghitung Luas Persegi Panjang"<<endl<<endl; cout<<"Masukkan Panjang = "; cin>>a; cout<<"Masukkan Lebar = "; cin>>b; sd.setA(a); sd.setB(b); cout<<"Luas Persegi Panjang = "<<sd.PersegiPanjang()<<endl<<endl<<endl; system("PAUSE"); return EXIT_SUCCESS; }
No comments:
Post a Comment