Professional Documents
Culture Documents
Programming
(OOP)
Class (C++)
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
}
Example of class
CRectangle
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void setValues (int a,int b);
int area ();
};
void CRectangle::setValues (int a,
int b) {
x = a;
y = b;
}
int CRectangle:: area () {
return (x*y);
}
int main () {
CRectangle rect;
rect.setValues (3,4);
cout << "area: " <<
rect.area();
return 0;
}
Example of class
CRectangle
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void setValues (int a,int b);
int area ();
};
void CRectangle::setValues (int a,
int b) {
x = a;
y = b;
}
int CRectangle:: area () {
return (x*y);
}
int main ()
{
Example of class
CRectangle
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void setValues (int a,int b);
int area ();
};
void CRectangle::setValues (int
a, int b) {
x = a;
y = b;
}
int CRectangle:: area () {
return (x*y);
}
int main () {
CRectangle *rect = new
Crectangle();
rect->setValues (3,4);
cout << "area: " << rect>area();
return 0;
}
namespace ExampleClass
{
class CRectangle {
int x, y;
public void setValues (int a,
int b) {
x = a;
y = b;
}
public int area() {
return (x * y);
}
}
class Program
{
static void Main(string[] args)
{
CRectangle recta = new
CRectangle();
Crectangle rectb = new
CRectangle();
recta.setValues(3, 4);
rectb.setValues(5, 6);
Console.WriteLine("rect a:{0}",
recta.area());
Console.WriteLine("rect b:{0}",
rectb.area());
}
}
namespace ExampleClass
{
class CRectangle
{
int x, y;
public void setValues (int a,
int b)
{
x = a;
y = b;
}
public int area() {
return (x * y);
}
package ExampleClass;
class CRectangle
{
int x, y;
public void setValues (int
a, int b)
{
x = a;
y = b;
}
public int area() {
return (x * y);
}
Constructor
s
Constructors and
destructors
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
CRectangle (int, int);
int area ();
};
CRectangle:: CRectangle(int a, int
b) {
width = a;
height = b;
}
int CRectangle:: area () {
return (width*height);
}
int main () {
return 0;
}
Constructors and
destructors(C#)
namespace ExampleClass
{
class CRectangle {
int width, height;
public CRectangle (int a, int b)
{
width = a;
height = b;
}
public int area() {
return (width*height);
}
}
class Program
{
static void Main(string[] args)
{
CRectangle recta = new
CRectangle(3,4);
CRectangle rectb = new
CRectangle(5,6);
Console.WriteLine("rect a:{0}",
recta.area());
Console.WriteLine("rect b:{0}",
rectb.area());
}
}
Constructors and
destructors(Java)
package ExampleClass;
class CRectangle
{
int width, height;
public CRectangle (int a,
int b) {
width = a;
height = b;
}
public int area() {
return
(width*height);
}
Overloading
Constructors
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
CRectangle ();
CRectangle (int, int);
int area ();
};
CRectangle::CRectangle () {
width = 5;
height = 5;
}
CRectangle:: CRectangle(int a, int
b) {
width = a;
height = b;
}
int CRectangle:: area () {
return (width*height);
}
int main () {
return 0;
}
namespace ExampleClass
{
class CRectangle {
int width, height;
public CRectangle () {
width = 5;
height = 5;
}
public CRectangle (int a, int b)
{
width = a;
height = b;
}
public int area() {
return (width*height);
}
}
class Program
{
static void Main(string[] args)
{
CRectangle recta = new
CRectangle(3,4);
CRectangle rectb = new
CRectangle();
Console.WriteLine("rect a:{0}",
recta.area());
Console.WriteLine("rect b:{0}",
rectb.area());
}
}
package ExampleClass;
class CRectangle
{
int width, height;
public CRectangle () {
width = 5;
height = 5;
}
public CRectangle (int a,
int b) {
width = a;
height = b;
}
public int area() {
return
(width*height);
}
Example of Default
constructor
#include <iostream>
using namespace std;
class CRectangle {
int x, y;
public:
void setValues (int a,int b);
int area ();
};
void CRectangle::setValues (int
a, int b) {
x = a;
y = b;
}
int CRectangle:: area () {
return (x*y);
}
int main () {
CRectangle *rect = new
Crectangle();
rect->setValues (3,4);
cout << "area: " << rect>area();
return 0;
}
#include <iostream>
using namespace std;
class CRectangle {
int width, height;
public:
CRectangle ();
CRectangle (int, int);
int area ();
};
CRectangle::CRectangle () {
width = 5;
height = 5;
}
CRectangle:: CRectangle(int a, int
b) {
width = a;
height = b;
}
int CRectangle:: area () {
return (width*height);
}
int main () {
CRectangle rectb;
return 0;
}
Destructors
#include <iostream>
using namespace std;
class CRectangle {
int *width, *height;
public:
CRectangle (int, int);
~CRectangle ();
int area ();
};
CRectangle:: CRectangle(int a, int
b) {
witdh = new int;
height = new int;
width = a;
height = b;
}
CRectangle:: ~CRectangle(){
delete width;
delete height;
}
int main () {
return 0;
}
Pointers to
classes
Example of Pointers to
classes
#include <iostream>
using namespace std;
class CRectangle {
int w, h;
public:
void setValues (int a,int b);
int area ();
};
void CRectangle::setValues (int
a, int b) {
w = a;
h = b;
}
int CRectangle:: area () {
return (w*h);
}
int main () {
CRectangle recta;
CRectangle *rectb;
rectb = &recta
cout << "area: " << recta>area();
cout << "area: " << rectb>area();
return 0;
}
Example of Pointers to
classes
#include <iostream>
using namespace std;
class CRectangle {
int w, h;
public:
void setValues (int a,int b);
int area ();
};
void CRectangle::setValues (int
a, int b) {
w = a;
h = b;
}
int CRectangle:: area () {
return (w*h);
}
int main () {
CRectangle *rectb;
CRectangle *rectc = new
CRectangle();
rectc->setValues (8,9);
rectb = rectc;
cout << "area: " << rectb>area();
cout << "area: " << rectc>area();
return 0;
}
#include <iostream>
using namespace std;
class CRectangle {
int w, h;
public:
void setValues (int a,int b);
int area ();
};
void CRectangle::setValues (int
a, int b) {
w = a;
h = b;
}
int CRectangle:: area () {
return (w*h);
}
int main () {
CRectangle recta;
recta->setValues (3,4);
CRectangle *rectb;
rectb = &recta
rectb->setValues (3,9);
cout << "area: " << recta>area();
cout << "area: " << rectb>area();
CRectangle *rectc = new
Crectangle();
rectc->setValues (5,6);
rectb = rectc;
rectc->setValues (7,8);
cout << "area: " << rectb>area();
cout << "area: " << rectc>area();
(C#)
namespace ExampleClass
{
class CRectangle {
int width, height;
public CRectangle (int a, int b)
{
width = a;
height = b;
}
public int area() {
return (width*height);
}
}
class Program
{
static void Main(string[] args)
{
CRectangle recta = new
CRectangle(3,4);
CRectangle rectb = new
CRectangle(5,6);
recta=rectb;
Console.WriteLine("rect a:{0}",
recta.area());
Console.WriteLine("rect b:{0}",
rectb.area());
}
}
Java
package ExampleClass;
class CRectangle
{
int width, height;
public CRectangle (int a,
int b) {
width = a;
height = b;
}
public int area() {
return
(width*height);
}
#include <iostream>
using namespace std;
class CRectangle {
int w,h;
CRectangle *rect;
public:
void setValues (CRectangle
*rect);
void setValues (int a, int b);
int area ();
};
void CRectangle::setValues (int a, int
b) {
w = a;
h = b;
}
void CRectangle::setValues
(CRectangle *r) {
rect = r;
}
int main () {
CRectangle *recta = new
CRectangle();
recta->setValues (3,4);
CRectangle rectb;
rectb.setValues (recta);
return 0;
}
Inheritance
class CRectangle {
protected:
int width, height;
public:
void setValues (int a, int b);
public:
int area ();
};
void CRectangle::setValues (int a,
int b){
width=a;
height=b;
}
int CRectangle: :area (){
return (width * height);
}
class CTriangle{
protected:
int width, height;
public:
void setValues (int a, int b);
int area ();
};
void CTriangle ::setValues (int a, int
b){
width=a;
height=b;
}
int CTriangle:: area (){
return (width * height / 2);
}
int main () {
CRectangle rect; CTriangle trgl;
rect.setValues (4,5);
trgl.setValues (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}
class CPolygon {
protected:
int width, height;
public:
void setValues (int a, int b);
};
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
class CRectangle: public
CPolygon {
public:
int area ();
};
int CRectangle: :area (){
return (width * height);
}
int main () {
CRectangle rect;
CTriangle trgl;
rect.setValues (4,5);
trgl.setValues (4,5);
protected:
int width, height;
public:
void setValues (int a, int b);
};
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
protected:
int width, height;
public:
void setValues (int a, int b);
};
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
public:
int area ();
};
int CRectangle: :area (){
return (width * height);
}
public:
int area ();
rect
trgl
};
protected:
int width, height;
public:
void setValues (int a, int b);
};
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
public:
int area ();
};
int CRectangle: :area (){
return (width * height);
}
int main () {
CRectangle rect;
rect.setValues (4,5);
protected:
int width, height;
public:
void setValues (int a, int b);
};
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
public:
int area ();
};
int main () {
CTriangle trgl;
trgl.setValues (4,5);
Access
class A{
private:
int x;
protected:
int y;
public:
int z;
void print();
};
void CPolygon :: print(){
cout<<x;cout <<y;cout<z;
}
class B: public A{
public:
int print();
};
void CPolygon :: print(){
cout<<x; cout <<y; cout<<z;
}
int main () {
A ma;
class CPolygon {
protected:
int width, height;
public:
void setValues (int a, int b);
};
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
class CRectangle: public
CPolygon {
public:
int area ();
};
int CRectangle: :area (){
return (width * height);
}
int main () {
CRectangle rect;
CTriangle trgl;
rect.setValues (4,5);
trgl.setValues (4,5);
Inheritance
C#
class Polygon {
protected int width,
height;
public void setValues
(int a, int b){
width=a;
height=b;
}
class Rectangle: Polygon {
public int area (){
return (width *
height);
}
class Program {
static void Main(string[] args){
Rectangle rect = new Rectangle ();
Triangle trgl = new Triangle ();
rect.setValues (4,5);
trgl.setValues (4,5);
Console.WriteLine("rect a:{0}",
recta.area());
Console.WriteLine(trglb:{0}",
trgl.area());
return 0;
}
}
Inheritance
Java
return 0;
}
}
Inheritance
&
Constructors
class CPolygon {
protected:
int width, height;
public:
CPolygon(int a);
void setValues (int a, int b);
};
CPolygon :: CPolygon(int a){
width=a;
}
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
class Polygon {
protected int width,
height;
public Polygon(int a){
width=a;
}
public void setValues (int
a, int b){
width=a;
height=b;
}
}
C#
return;
}
}
class Polygon {
protected int width,
height;
public Polygon(int a){
width=a;
}
public void setValues (int
a, int b){
width=a;
height=b;
}
}
Java
return;
}
}
package M;
public class A{
int x;
private int y;
protected int i;
public int j;
public void print(){
Console.WriteLine(x + y + i +
j);
}
class X{
public void writeln(){
Console.WriteLine(x + y
+ i + j);
}
}
};
package M;
public class B extends A{
public int print(){
Console.WriteLine(x + y
+ i + j);
}
package N;
public class C extends A{
public int print(){
Console.WriteLine(x + y +
i + j);
}
}
package M;
static void Main(string[] args){
A ma = new A();
Console.WriteLine(ma.x);
Console.WriteLine(ma.y);
Console.WriteLine(ma.i);
Console.WriteLine(ma.j);
}
package N;
static void Main(string[] args){
A ma = new A();
Console.WriteLine(ma.x);
Console.WriteLine(ma.y);
Console.WriteLine(ma.i);
Console.WriteLine(ma.j);
}
Multiple
inheritance
class CPolygon {
protected:
int width, height;
public:
void setValues (int a, int
b);
};
void CPolygon :: setValues (int
a, int b){
width=a;
height=b;
}
class COutput {
public:
void writeln(int i);
};
void COutput::writeln (int i) {
cout << i << endl;
}
class USBDevice {
long m_id;
public:
USBDevice(long id);
long getID() ;
};
USBDevice::USBDevice(long id):
m_id(id) {
}
long USBDevice::getID() {
return m_id;
}
class NetworkDevice{
long m_id;
public:
NetworkDevice(long id);
long getID() ;
};
NetworkDevice::NetworkDevice(long
id): m_id(id){
}
long NetworkDevice ::getID() {
return m_lID;
}
int main()
{
WirelessAdaptor c54G(5442,
181742);
cout << c54G.getID();
return 0;
}
class USBDevice {
long m_id;
public:
USBDevice(long id);
long getID() ;
};
USBDevice::USBDevice(long id):
m_id(id) {
}
long USBDevice::getID() {
return m_id;
}
class NetworkDevice{
long m_id;
public:
NetworkDevice(long id);
long getID() ;
};
NetworkDevice::NetworkDevice(long
id): m_id(id){
}
long NetworkDevice ::getID() {
return m_id;
}
int main()
{
WirelessAdaptor c54G(5442,
181742);
cout <<
c54G.USBDevice::getID();
return 0;
}
class PoweredDevice{
int a;
public:
PoweredDevice();
void print();
};
PoweredDevice::PoweredDevice(){
a=9999;
}
void PoweredDevice::print(){
cout<<a;
}
class Scanner: public
PoweredDevice{
};
class Printer: public
PoweredDevice{
};
class Copier: public Scanner,
public Printer{
int main () {
Copier mc();
mc.print();
return 0;
}
Multiple inheritance
Java & C#
interface IA {
void m1();
}
class C : IA{
void m1(){
Console.WriteLine(Interface A");
{
void m2(){
Console.WriteLine(Hello");
}
static void Main() {
IA a=new C();
a.m1();
a.m2();
}
}
interface IA {
void m1();
}
interface IB {
void m2();
}
class
class C : IA,IB{
void m1(){
Console.WriteLine(Interface
A");
{
void m2(){
Console.WriteLine(Interface
B");
}
void m3(){
Console.WriteLine(Hello");
}
}
interface IA {
void m1();
}
class B {
void m2(){
Console.WriteLine(Hello
B");
}
}
class
class C : B, IA{
void m1(){
Console.WriteLine(Interface
A");
{
void m3(){
Console.WriteLine(Hello");
}
}
interface IABC {
int x;
}
error CS0525: Interfaces cannot contain fields
interface IABC {
void xyz() {
System.Console.WriteLine("In xyz");
}
}
interface abc {
void xyz();
}
class Demo : abc {
void xyz() {
System.Console.WriteLine("In xyz");
}
}
error CS0536: 'Demo' does not implement interface member
'abc.xyz()'.'Demo.xyz()' is either static, not public,
or has the wrong return type.
interface Area
{
int compute (int x, int y);
}
class Rectangle implements Area
{
public int compute(int x, int
y)
{
return(x * y);
}
public int perimeter (int
x,int y)
{
return(x + y)*2;
}
}
class Triangle implements Area
{
public int compute (int
x,int y)
{
return(x * y/2);
class
InterfaceArea
{
public static void main(String
args[])
{
Rectangle rect = new
Rectangle();
Triangle tri = new Triangle();
Area area;
area = rect;
println("Rect = "+
area.compute(1,2));
area = tri;
println("Tria = "+
area.compute(10,2));
println(area. perimeter (10,2));
}
}
Abstract class
class CPolygon {
protected:
int width, height;
public:
void setValues (int a, int b);
virtual int area () =0;
};
void CPolygon :: setValues (int a,
int b){
width=a;
height=b;
}
class CRectangle: public
CPolygon {
public:
int area ();
};
int CRectangle: :area (){
return (width * height);
class Polygon {
protected int width,
height;
public Polygon(int a, int b)
{
width = a;
height = b;
}
public void setValues (int
a, int b){
width=a;
height=b;
}
}
Java
po.setValues(4,5);
return;
}
}
abstract class A {
abstract void callme();
void callmetoo() {
class AbstractDemo {
public static void main(String
args[]) {
B b = new B();
b.callme();
b.callmetoo();
A a = new A();
System.out.println(Hello 1");
}
}
class B extends A {
void callme() {
System.out.println(Hello 2");
}
}
}
}
Java
return;
}
}
class Program {
static void Main()
{
absClass a = new absClass ();
absDerived a = new absDerived
();
Write(a.Add(5,4));
Write(a.Mul(2,3));
absClass b = new absDerived ();
Write(b.Add(5,4));
Write(b.Mul(2,3));
}
}
class Program {
static void Main()
{
absClass a = new absClass ();
absClass2 a = new absClass2
();
absClass b = new absDerived();
Write(b.Add(5,4));
Write(b.Mul(2,3));
}
}
}
class absDerived: absClass {
class Program {
static void Main()
{
absClass b = new absDerived();
b.numbers = 10 ;
Console.Write(b.numbers);
}
}
VIRTUAL &
ABSTRACT
Console.WriteLine("Says goodbye!");
}
}
public class SayHello: Talk {
public override void speak() {
Console.WriteLine(" Says
Hello!");
}
}
class Program {
static void Main()
{
SayHello hello = new SayHello();
hello.speak();
hello.goodbye();
}
}
Says Hello!
Says
goodbye!
Polymorphism
class Nguoi
{
public:
virtual void Chao() ;
};
virtual void Nguoi ::Chao() {
cout << "Toi chua biet chao";
};
class NguoiViet : public Nguoi
{
public:
void Chao();
};
void NguoiViet ::Chao() {
cout << "Xin chao.";
}
STATIC
Static Variable
class Student{
string name;
public void SetName(String n){
Name is : Nguyen
name =n;
Van A
}
public void GetName(){
Name is : Nguyen
return name ;
Van B
}
}
public class Program{
public static void Main( string[] args ) {
Student sdA= new Student();
Student sdB= new Student();
sdA.SetName(Nguyen Van A);
sdB.SetName(Nguyen Van B);
Console.WriteLine( Name is: " + sdA.getName());
Console.WriteLine( Name is: " + sdB.getName());
}
}
Static Variable
class Student{
static string name;
public void SetName(String n){
Name is : Nguyen
name =n;
Van A
}
public void GetName(){
Name is : Nguyen
return name ;
Van A
}
}
public class Program{
public static void Main( string[] args ) {
Student sdA= new Student();
Student sdB= new Student();
sdA.SetName(Nguyen Van A);
sdB.SetName(Nguyen Van B);
Console.WriteLine( Name is: " + sdA.getName());
Console.WriteLine( Name is: " + sdB.getName());
}
}
Static Method
public class MaximumFinder {
public int Determine( int x, int y) {
if(x>y)
retrun x;
else
return y;
}
}
public class Program{
public static void Main( string[] args ) {
MaximumFinder maxFinder = new MaximumFinder();
int result = maxFinder.Determine(5,7);
Console.WriteLine( "Maximum is: " + result );
}
}
Static Method
class Program {
static void Main()
{
Console.WriteLine(Nhap mot so bat ky:");
string line = Console.ReadLine();
int value = int.parse(line);
Console.WriteLine(Ban vua nhap so : ");
Console.WriteLine(value * 10 );
Static Method
public class MaximumFinder {
public static int Determine( int x, int y) {
if(x>y)
retrun x;
else
return y;
}
}
public class MaximumFinderTest {
public static void Main( string[] args ) {
int result = MaximumFinder.Determine(5,7);
Console.WriteLine( "Maximum is: " + result );
}
}
ERROR
OK
OK
Bi tp
Vit chng trnh sp xp cc loi
kiu d liu nh mng s nguyn
hoc sp xp cc sinh vin theo im
trung bnh
class Program
{
static void Main(string[] args) {
int []sn = new int[10];
sn[0]= 5;
sn[1]= 6;
sn[2]= 3;
SapXep sx = new SapXep()
sn.SapXepTang(sn,3);
sn.Print(sn,3);
sn.SapXepGiam(sn,3);
sn.Print(sn,3);
}
}
class Program
{
static void Main(string[] args) {
{
for (int i = 0;i<n;i++) {
for (int j = i + 1; j < n; j++){
ISoSanh x = (ISoSanh)a[i];
ISoSanh y = (ISoSanh)a[j];
if (x.SoSanh(y)==true){
object t = a[i]; a[i] = a[j]; a[j] =
t;
}
}
}
}
}
class Program
{
static void Main(string[] args) {
SoNguyen []sn = new SoNguyen[10];
{
for (int i = 0;i<n;i++) {
for (int j = i + 1; j < n; j++){
ISoSanh x = (ISoSanh)a[i];
ISoSanh y = (ISoSanh)a[j];
if (x.SoSanh(y)==true){
object t = a[i]; a[i] = a[j]; a[j] =
t;
}
}
}
}
}
class Program
{
static void Main(string[] args) {
SinhVien []sv = new SinhVien[10];
sv[0]= new SinhVien("A",5);
sv[1]= new SinhVien("B",6);
sv[2]= new SinhVien("C",3);
SapXep.SapXepTang(sv,3);
}
}