You are on page 1of 88

Object Oriented

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;
}

private, public or protected


public members are accessible from
anywhere where the object is visible.
private members of a class are accessible
only from within other members of the
same class or from their friends.
By default, all members of a class declared
with the class keyword have private
protected members are accessible from
members of their same class and from their
friends, but also from members of their
derived classes.

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 recta, rectb;


recta.setValues (3,4);
rectb.setValues (5,6);
cout<<"area
a:"<<recta.area();
cout<<"area
b:"<<rectb.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 () {
CRectangle *rect = new
Crectangle();
rect->setValues (3,4);
cout << "area: " << rect>area();
return 0;
}

Example of class Crectangle


(C#)

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());
}
}

Example of class Crectangle


(C#)

namespace ExampleClass

{
class CRectangle
{
int x, y;
public void setValues (int a,
int b)
{
x = a;
y = b;
}
public int area() {
return (x * y);
}

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());
}
}

Example of class Crectangle


(Java)

package ExampleClass;
class CRectangle

{
int x, y;
public void setValues (int
a, int b)
{
x = a;
y = b;
}
public int area() {
return (x * y);
}

public static void main(String[] args)


{
CRectangle rect = new
CRectangle();
CRectangle rectb = new
CRectangle();
rect.setValues(3, 4);
rect.setValues(5, 6);
System.out.println("rect a:"+
rect.area());
System.out.println("rect b:"+
rectb.area());
}

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 () {

CRectangle recta (3,4);


CRectangle rectb (5,6);
cout<<"area
a:"<<recta.area();
cout<<"area
b:"<<rectb.area();

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);
}

public static void main(String[] args)


{
CRectangle recta = new
CRectangle(3,4);
CRectangle rectb = new
CRectangle(5,6);
System.out.println("rect a:"+
recta.area());
System.out.println("rect b:"+
rectb.area());
}

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 () {

CRectangle recta (3,4);


CRectangle rectb;
cout<<"area
a:"<<recta.area();
cout<<"area
b:"<<rectb.area();

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);
}

public static void main(String[] args)


{
CRectangle recta = new
CRectangle(3,4);
CRectangle rectb = new
CRectangle();
System.out.println("rect a:"+
recta.area());
System.out.println("rect b:"+
rectb.area());
}

If you do not declare any


constructors in a class definition, the
compiler assumes the class to have a
default constructor with no
arguments.

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;

CRectangle recta (3,4);


cout<<"area
a:"<<recta.area();
cout<<"area
b:"<<rectb.area();

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 CRectangle:: area ()


{
return
(width*height);
}

int main () {

CRectangle recta (3,4);


cout<<"area
a:"<<recta.area();

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);
}

public static void main(String[] args)


{
CRectangle recta = new
CRectangle(3,4);
CRectangle rectb = new
CRectangle(5,6);
recb=recta;
System.out.println("rect a:"+
recta.area());
System.out.println("rect b:"+
rectb.area());
}

#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);
}

class CTriangle: public CPolygon {


public:
int area ();
};

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;
}

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

};

int CTriangle:: area (){


return (width * height / 2);
}

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);

cout << rect.area() << endl;


return 0;
}

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 CTriangle:: area (){


return (width * height / 2);
}

int main () {
CTriangle trgl;
trgl.setValues (4,5);

cout << trgl.area() << endl;


return 0;
}

Access

publ protect private


ic
ed
same class
yes
yes
yes
derived classes yes
yes
no
not members
yes
no
no

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;

cout << ma.x << endl;


cout << ma.y << endl;
cout << ma.z << 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);
}

class CTriangle: public CPolygon {


public:
int area ();
};

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;
}

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 Triangle: Polygon {


public int area (){
return (width * height
/ 2);
}

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

public class Polygon {


protected int width, height;
public void setValues (int a,
int b){
width=a;
height=b;
}
public class Rectangle extends
Polygon {
public int area (){
return (width * height);
}

public class Triangle extends


Polygon {

public int area (){


return (width * height /
2);
}

public class Program {

public static void


main(String[] args)
Rectangle rect = new Rectangle ();
Triangle trgl = new Triangle ();
rect.setValues (4,5);
trgl.setValues (4,5);
System.out.println ("rect a:{0}",
recta.area());
System.out.println (trglb:{0}",
trgl.area());

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 CRectangle: public


CPolygon {
public:
CRectangle(int w, int h);
int area ();
};
CRectangle: : CRectangle(int w,
int h):
CPolygon (w)
{
height=h;
}
int CRectangle: :area (){
return (width * height);
}
int main () {
CRectangle rect(8,9);

cout << rect.area() << endl;


return 0;
}

class Polygon {
protected int width,
height;
public Polygon(int a){
width=a;
}
public void setValues (int
a, int b){
width=a;
height=b;
}
}

C#

class Rectangle: Polygon {


public Rectangle(int w, int
h):base(w){
height=h;
}
public int area (){
return (width * height);
}
}
class Program {
static void Main(string[] args){
Rectangle rect = new Rectangle
(8,9);
Console.WriteLine("rect a:{0}",
rect.area());

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

class Rectangle extends Polygon {


public Rectangle(int w, int h){
supper (w);
height=h;
}
public int area (){
return (width * height);
}
}
class Program {
static void Main(string[] args){
Rectangle rect = new Rectangle
(8,9);
Console.WriteLine("rect a:{0}",
rect.area());

return;
}
}

Access Modifiers (Java)

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 CRectangle: public CPolygon, public


COutput {
public:
int area ();
};
int CRectangle: :area (){
return (width * height);
}
class CTriangle: public CPolygon, public
COutput{
public:
int area ();
};
int CTriangle:: area (){
return (width * height / 2);
}
int main () {
CRectangle rect;
CTriangle trgl;
rect.setValues (4,5);
trgl.setValues (4,5);
rect.writeln(rect.area());
trgl.writeln(trgl.area());
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_lID;
}

class WirelessAdaptor: public


USBDevice, public NetworkDevice
{
public:
WirelessAdaptor(long uid, long nid)
:USBDevice(uid),
NetworkDevice(nid){
}
};

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;
}

class WirelessAdaptor: public


USBDevice, public NetworkDevice
{
public:
WirelessAdaptor(long uid, long nid)
:USBDevice(uid),
NetworkDevice(nid){
}
};

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");
}
}

static void Main() {


IA a=new C();
a.m1();
a.m2();
IB b = new C();
b.m2();
}

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");
}
}

static void Main() {


IA a=new C();
a.m1();
B b = new C();
b.m2();
}

interface IABC {
int x;
}
error CS0525: Interfaces cannot contain fields

interface IABC {
void xyz() {
System.Console.WriteLine("In xyz");
}
}

error CS0531: 'abc.xyz()': interface members cannot have a definition


interface Iabc {
void xyz();
}
class Demo : Iabc {
public void m1() {
System.Console.WriteLine("Hello Interfaces");
}
}
error CS0535: 'Demo' does not implement interface member 'Iabc.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));
}
}

Interface rules (Java)


All interface methods are implicitly public and
abstract. In other words, you do not need to actually
type the public or abstract modifiers in the method
declaration, but the method is still always public and
abstract.
All variables defined in an interface must be public,
static, and final, in other words, interfaces can declare
only constants, not instance variables.
Interface methods must not be static.
Because interface methods are abstract, they cannot
be marked final
An interface can extend one or more other interfaces.
An interface cannot extend anything but another
interface.
An interface cannot implement another interface or
class.

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 CTriangle: public


CPolygon {
public:
int area ();
};
int CTriangle:: area (){
return (width * height /
2);
}
int main () {
CRectangle rect;
CTriangle trgl;
CPolygon * ppoly1 =
&rect;
CPolygon * ppoly2 =
&trgl;
ppoly1->set_values (4,5);
ppoly2->set_values (4,5);
cout << ppoly1->area()
<< endl;

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

class Rectangle extends Polygon {


public Rectangle(int w, int h){
supper (w);
height=h;
}
public int area (){
return (width * height);
}
}
class Program {
static void Main(string[] args){
Polygon po = new Polygon (8,9);

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");

}
}

}
}

abstract class Polygon {


protected int width,
height;
public Polygon(int a){
width=a;
}
public void setValues (int
a, int b){
width=a;
height=b;
}
abstract public int area ();
}

Java

class Rectangle extends Polygon {


public Rectangle(int w, int h){
supper (w);
height=h;
}
public int area (){
return (width * height);
}
}
class Program {
static void Main(string[] args){
Rectangle rect = new Rectangle
(8,9);
Console.WriteLine("rect a:{0}",
rect.area());

return;
}
}

abstract class absClass {


public int Add(int x, int y) {
return x + y;
}
public abstract int Mul(int x,
int y);
}
class absDerived: absClass {
public override int Mul(int x,
int y){
return x * y;
}
}

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));
}
}

abstract class absClass {


public abstract int Add(int x,
int y) ;
public abstract int Mul(int x,
int y);
}
abstract class absClass2 :
absClass {
public override int Add(int x,
int y){
return x + y;
}
}
class absDerived: absClass2 {
public override int Mul(int x,
int y){
return x * y;
}
}

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));
}
}

abstract class absClass {

protected int myNumber;


public abstract int numbers
{
get;
set;
}

}
class absDerived: absClass {

public override int numbers


{
get {
return myNumber;
}
set {
myNumber = value;
}

class Program {
static void Main()
{
absClass b = new absDerived();
b.numbers = 10 ;
Console.Write(b.numbers);
}
}

VIRTUAL &
ABSTRACT

public abstract class Talk {


public abstract void speak();
public virtual void goodbye() {

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!

public abstract class Talk {


public abstract void speak();
public virtual void goodbye() {
Console.WriteLine("Says
goodbye!");
class Program {
}
static void Main()
}
{
public class SayHello: Talk {
SayHello hello = new
public override void speak()
SayHello();
{
hello.speak();
Console.WriteLine(" Says Hello!"); hello.goodbye();
}
}
public override void goodbye ()
}
{
WriteLine ("ClassHello says
goodbye!");
}
}

S KHC NHAU GIA


VIRTUAL & ABSTRACT C#
Lp Con khng nht thit phi Override
cho method Virtual lp Cha. Ngc li
Abstract th bt buc
Method Virtual phi ci t Abstract th
tuyt ti khng.
Abstract phi nm trong class abstract

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.";
}

class NguoiAnh : public Nguoi {


public:
void Chao() {
cout << "Hello.";
}
};
int main() {
Nguoi *n;
NguoiViet nv;
NguoiAnh na;
n = &nv;
n->Chao(); // (*)
n = &na;
n->Chao(); // cng dng code
voi
(*) nhng li cho kt qu
khc
return 0;
}

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 );
}
}

Static Method (Note)


public class A{
int i;
static int j;
public static int MethodX(int y) {
i = y;
}
public static int MethodY(int y) {
j = y;
}
public int MethodZ(int y) {
j = y;
}
}

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

public class SapXep{


public void SapXepTang(int []a, int n){
for (int i = 0;i<n;i++) {
for (int j = i + 1; j < n; j++){
if (a[i]>a[j]){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
public void SapXepGiam(int []a, int n){
...
if (a[i]<a[j]){
...
}
public void Print(int []a, int n){
for (int i = 0;i<n;i++) {
write(a[i]));
}
}

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);
}
}

public class SapXep{


public void SapXepTang(float []a, int n){
for (int i = 0;i<n;i++) {
for (int j = i + 1; j < n; j++){
if (a[i]>a[j]){
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
}
public void SapXepGiam(float[]a, int n){
...
if (a[i]<a[j]){
...
}
public void Print(float []a, int n){
for (int i = 0;i<n;i++) {
write(a[i]));
}
}

class Program

{
static void Main(string[] args) {

float []sn = new float [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);
}
}

public interface ISoSanh{


bool SoSanh(object a);
}
public class SoNguyen:
ISoSanh{
int data;
public float Data {
get { return data; }
set { data = value; }
}
public SoNguyen( int d){
data = d;
}
public bool
SoSanh(object a) {
SoNguyen sv =
(SoNguyen)a;
if (sv.Data > data)
return true;
else
return false;
}
}

public class SapXep{


public static void SapXepTang(object []a, int n)

{
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];

sn[0]= new SoNguyen(5);


sn[1]= new SoNguyen(6);
sn[2]= new SoNguyen(3);
SapXep.SapXepTang(sn,3);
}

public interface ISoSanh{


bool SoSanh(object a);
}
public class SinhVien : ISoSanh{
string ten;
float dtb;
public string Ten{
get { return ten; }
set { ten = value; }
}
public float Diem {
get { return dtb; }
set { dtb = value; }
}
public SinhVien( string t,
float d){
ten =t;
dtb =d;
}
public bool SoSanh(object a)
{
SinhVien sv = (SinhVien)a;
if (sv.Diem > dtb)
return true;
else
return false;

public class SapXep{


public static void SapXepTang(object []a, int n)

{
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);
}
}

You might also like