Navigation
 Index
 Memberlist
 Profile
 FAQ
 Search
Latest topics
» Perkhidmatan Kereta Sewa Shah Alam
Wed Sep 09, 2015 2:48 am by sksportcar

» Homestay Di Kota Bharu
Sun Jan 25, 2015 10:52 pm by sksportcar

» Kereta Sewa Shah Alam
Tue Aug 05, 2014 8:54 am by sksportcar

» Bahasa Jepun: Ayat Seharian,Hari,Haribulan & Nombor
Mon Sep 23, 2013 8:43 pm by foxfoxer

» Tips nak hapuskan virus secara manual [On updates]
Tue Mar 19, 2013 8:38 am by Optitech

» Teknik defense dari virus and spyware [Auto]
Thu Jul 21, 2011 2:53 am by Optitech

» Virus shortcut kat pendrive
Thu Jul 21, 2011 12:27 am by Optitech

» Admin Kembali dengan Menyengat
Wed Jul 20, 2011 1:59 pm by Optitech

» Xcode Magazine - Penulisan daripada hacker
Wed Jul 20, 2011 12:45 pm by Optitech


CODING LEARNING & SAMPLE

View previous topic View next topic Go down

CODING LEARNING & SAMPLE

Post  Optitech on Wed Mar 31, 2010 11:58 am

Dibawah ini merupakan beberapa sample kod bagi setiap tajuk bagi kelas programming.


1.INTRODUCTION
Spoiler:

#include <iostream>
using namespace std;

class sport {
public:
int badminton;
int rugby;
int diving;
int martialart;
};

int main()

{
sport boys, girls; //

cout<<"\t\t--------------------------"<<endl;
cout<<"\t\tSPORT LIST"<<endl;
cout<<"\t\tStudent participants"<<endl;
cout<<"\t\t--------------------------"<<endl<<endl;

//----------------------

int total1;

cout<<"BADMINTON:"<<endl;
cout<<"----------"<<endl;
cout<<"Guys:";
cin>>boys.badminton;

cout<<"Girls:";
cin>>girls.badminton;

total1=boys.badminton + girls.badminton;


cout<<endl<<endl;
//-----------------------

int total2;

cout<<"RUGBY:"<<endl;
cout<<"-------"<<endl;
cout<<"Guys:";
cin>>boys.rugby;

cout<<"Girls:";
cin>>girls.rugby;

total2=boys.rugby + girls.rugby;


cout<<endl<<endl;
//-----------------------

int total3;

cout<<"DIVING:"<<endl;
cout<<"-------"<<endl;
cout<<"Guys:";
cin>>boys.diving;

cout<<"Girls:";
cin>>girls.diving;

total3=boys.diving + girls.diving;


cout<<endl<<endl;
//----------------------

int total4;

cout<<"MARTIAL ART:"<<endl;
cout<<"------------"<<endl;
cout<<"Guys:";
cin>>boys.martialart;

cout<<"Girls:";
cin>>girls.martialart;

total4=boys.martialart + girls.martialart;


cout<<endl<<endl;
//----------------------

system("cls");
cout<<"SPORT\t\tPARTICIPANTS"<<endl;
cout<<"-----\t\t------------"<<endl;
cout<<"Badminton:\t"<<total1<<endl;
cout<<"Rugby:\t\t"<<total2<<endl;
cout<<"Diving:\t\t"<<total3<<endl;
cout<<"Martial Arts:\t"<<total4<<endl<<endl;

return 0;
}


2.MEMBER FUNCTIONS
Spoiler:

//Porgram to demonstrate methods and inline functions defined within class body

#include <iostream>
using namespace std;

class Time
{
public:
int hour, minute, second;
public:
//methods within class body also be known as implicit inline functions
void setTime(int x, int y, int z)
{
hour=x;
minute=y;
second=z;
}

};
//Main function that consist object declaration and statements
void main()
{
Time Malaysia;
Malaysia.setTime(10,5,20);

cout<<"--------"<<endl;
cout<<"MALAYSIA"<<endl;
cout<<"--------"<<endl;
cout<<Malaysia.hour<<".";
cout<<Malaysia.minute<<".";
cout<<Malaysia.second<<"PM"<<endl;
}


3.STATIC DATA MEMBER
Spoiler:


NO1
#include <iostream>
using namespace std;

class sem2A {
public:
static char class_name[4];
char student_name[15];
void showdata()
{
cout<<"student name:" <<student_name<<"; class:" <<class_name<<";"<<endl;

}
};
char sem2A::class_name[4];

int main() {
sem2A female, male;
strcpy (female.student_name, "Nabila");
strcpy (male.student_name, "Hishamudin");
strcpy (male.class_name, "2B");

male.showdata();
female.showdata();

cout<<endl;

return 0;
}


NO2

#include <iostream>
using namespace std;

class Test
{
public:
static int num;
void shownum()
{
cout<<num<<endl;
}
};
int Test::num;

int main() {
Test a,b;
Test::num=100;

a.shownum();
b.shownum();

return 0;

}

NO3

#include<iostream>
using namespace std;

class StaticTest
{
private:
static int x;
public:
static int count()
{
return x;
}
};

int StaticTest:Mad=9;

void main()
{
cout<<StaticTest::count()<<endl;
}


CONSTRUCTOR AND DESTRUCTOR
Spoiler:

NO1
#include <iostream>
using namespace std;

class Object
{
int field;
public:

Object();
~Object();
};

Object::Object()
{
field=0;
cout<<"Object Created"<<endl;
}

Object::~Object()
{
cout<<"Object Destroyed!"<<endl;
}

int main()
{
Object no1, no2;
}


NO2

#include <iostream>
using namespace std;

class MyClass
{
public:
int x;

MyClass();
~MyClass();
};

MyClass::MyClass()
{x=10;}

MyClass::~MyClass()
{
cout<<"Destructing..."<<endl;

}

int main()
{
MyClass ob1;
MyClass ob2;
cout<<ob1.x<<" "<<ob2.x<<endl;
}

NO3

#include <iostream>
using namespace std;

class exforsys
{
private:
int no1,no2;

public:
void test()
{
no1=100;
no2=200;
}
friend int compute(exforsys b1);
};

int compute(exforsys b1)
{
return int(b1.no1+b1.no2)-5;
}

void main()
{
exforsys b;
b.test();
cout<<"The result is:"<<compute(b)<<endl;
}

_________________
|----------------------------------------------------|




EXCUSE IS THE DUMBEST WAY TO ESCAPE

MAKLUMAN: Worksheet/Assignment Download Section hanya untuk pengguna berdaftar. Sila daftar untuk melakukan 'download'. Lepas dah click link, tekan kat perkataan "Skip Ad" bahagian atas belah kanan sekali. Pastu baru leh download.
Harap Maklum.

Optitech

Posts : 518
Join date : 2010-01-01
Age : 24
Location : ProxyFireZ

http://sembangseposen.blogspot.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum