Class (computer programming): Difference between revisions

Content deleted Content added
Notifying of internal link to section Abstract and concrete classes from PHP
Examples: made consistant -- four spaces for indents (removed tab characters), used <pre></pre> tags
Line 184:
==== Example 1 ====
 
<pre>
#include <iostream>
#include <stringiostream>
#include <string>
 
class Hello
class Hello
{
{
std::string what;
std::string what;
 
public:
public:
Hello(const char* s)
: what(s)
{
}
 
void say()
{
std::cout << "Hello " << what << "!" << std::endl;
}
};
 
int main( int argc, char** argv )
{
{
Hello hello_world("world");
hello_world.say();
 
return 0;
}
}
</pre>
 
This example shows how to define a [[C++]] class named "Hello". It has a private string attribute named "what", and a public method named "say".
Line 215 ⟶ 217:
==== Example 2 ====
 
<pre>
class MyAbstractClass
class MyAbstractClass
{
{
public:
public:
virtual void MyVirtualMethod() = 0;
virtual void MyVirtualMethod() = 0;
};
};
 
class MyConcreteClass : public MyAbstractClass
class MyConcreteClass : public MyAbstractClass
{
{
public:
public:
void MyVirtualMethod()
void {MyVirtualMethod()
{
//do something
}
};
</pre>
 
An object of class MyAbstractClass cannot be created because the function MyVirtualMethod has not been defined (the =0 is C++ syntax for a pure virtual function, a function that must be part of any derived concrete class but is not defined in the abstract base class. The MyConcreteClass class is a concrete class because its functions (in this case, only one function) have been declared and implemented.
Line 234 ⟶ 238:
==== Example 3 ====
 
<pre>
#include <string>
#include <iostream>
using std::string;
 
class InetMessage
{
string m_subject, m_to, m_from;
 
public:
InetMessage (const string& subject,
const string& to,
const string& from) : m_subject(subject), m_to(to), m_from(from) { }
string subject () const { return m_subject; }
string to () const { return m_to; }
string from () const { return m_from; }
virtual void printTo(std::ostream &out)
{
out << "Subject: " << m_subject << std::endl;
out << "From: " << m_from << std::endl;
out << "To: " << m_to << std::endl;
}
};
</pre>
 
=== Java ===
:''For more information on the programming language, see [[Java (programming language)]]. For more information on Java .class files, see [[Class (file format)]].''
<blockquote>
''For more information on the programming language, see [[Java (programming language)]]. For more information on Java .class files, see [[Class (file format)]].''
</blockquote>
 
==== Example 1 ====
 
<pre>
public class Example1
public class Example1
{
{
// This is a Java class, it automatically extends the class Object
// This is a Java class, it automatically extends the class Object
public static void main (String args[]) {
public static void main (String args[]) {
System.out.println("Hello world!");
System.out.println("Hello world!");
}
}
}
}
</pre>
 
This example shows the simplest [[Java (programming language)|Java]] class possible.
Line 276 ⟶ 282:
==== Example 2 ====
 
<pre>
public class Example2 extends Example1
public class Example2 extends Example1
{
{
// This is a class that extends the class created in Example 1.
// This is a class that extends the class created in Example 1.
protected int data;
protected int data;
 
public Example2()
public Example2()
{
{
// This is a constructor for the class. It does not have a return type.
// This is a constructor for the class. It does not have a return type.
data = 1;
data = 1;
}
}
 
public int getData()
public int getData()
{
{
return data;
}
 
public void setData(int d)
{
data = d;
}
}
</pre>
 
This example shows a class that has a defined constructor, one member data, an accessor method (getData) and a Modifier method (setData) for that member data. It extends the previous example's class. Note that in Java all classes automatically extend the class Object. This allows you to write generic code to deal with objects of any type.
Line 302 ⟶ 310:
=== REALbasic ===
==== Example 1====
 
Class Hello
<pre>
Private Dim what as String
Class Hello
Private Dim what as String
 
Sub Constructor( s as String )
what = s
End Sub
 
Sub Say()
MsgBox "Hello " + what + EndOfLine
End Sub
End Class
Sub App.Open()
Sub Constructor( s as String )
what = s
End Sub
Sub Say()
MsgBox "Hello " + what + EndOfLine
End Sub
End Class
Sub App.Open()
dim h as new Hello( "Foobar" )
h.Say
End Sub
</pre>
 
This example is a port of the C++ example above. It demonstrates how to make a class named Hello with a private property named what. It also demonstrates the proper use of a constructor, and has a public method named Say. It also demonstrates how to instantiate the class and call the Say method.
Line 326 ⟶ 337:
<?php
class A {
public function foo() {
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ")\n";
} else {
echo "\$this is not defined.\n";
}
}
}
?>
Line 340 ⟶ 351:
 
====Example 2====
 
<pre>
<?php
class date {
 
private $date;
 
public function __construct() {
$this->date = date('c');
}
 
private function getDate() {
return $this->date;
}
 
public function printDate() {
echo 'Todays Date is ' . $this->getDate() . '\n';
}
}
?>
Line 363 ⟶ 375:
===C#===
====Example 1====
 
<pre>
using System;
Line 413 ⟶ 426:
class Cart {
 
private var cart:Array;
 
function Cart() {
this.cart = new Array();
}
}
 
public function addItem(id:Number, name:String):Void {
this.cart.push([id, name]);
}
}
 
public function removeItemById(id:Number):Void {
var ln:Number = this.cart.length;
for (var i:Number = 0; i<ln; i++) {
var curr:Array = this.cart[i];
if (curr[0] == id) this.cart.splice(i, 1);
}
}
}
}
 
public function removeItemByName(name:String):Void {
var ln:Number = this.cart.length;
for (var i:Number = 0; i<ln; i++) {
var curr:Array = this.cart[i];
if (curr[1] == name) this.cart.splice(i, 1);
}
}
}
}
}
</pre>