View Category
Define a class
Declare a class named Greeter that takes a string on creation and greets using this string if you call the
"greet" method.
ruby
class Greeter
def initialize(whom) @whom = whom end
def greet() puts "Hello, #{@whom}!" end
end
(Greeter.new("world")).greet()
def initialize(whom) @whom = whom end
def greet() puts "Hello, #{@whom}!" end
end
(Greeter.new("world")).greet()
csharp
using System;
class Greeter
{
private string name {get;set;}
public void Greet(){
Console.WriteLine("Hello, {0}",name);
}
public Greeter(string name){
this.name = name;
}
}
class Test
{
static void Main()
{
new Greeter("Dante").Greet();
}
}
class Greeter
{
private string name {get;set;}
public void Greet(){
Console.WriteLine("Hello, {0}",name);
}
public Greeter(string name){
this.name = name;
}
}
class Test
{
static void Main()
{
new Greeter("Dante").Greet();
}
}
cpp
class Greeter
{
public:
Greeter(const std::string& whom);
void greet() const;
private:
std::string whom;
};
int main()
{
Greeter* gp = new Greeter("world");
gp->greet();
delete gp;
}
Greeter::Greeter(const std::string& whom) : whom(whom) {}
void Greeter::greet() const
{
std::cout << "Hello, " << whom << std::endl;
}
{
public:
Greeter(const std::string& whom);
void greet() const;
private:
std::string whom;
};
int main()
{
Greeter* gp = new Greeter("world");
gp->greet();
delete gp;
}
Greeter::Greeter(const std::string& whom) : whom(whom) {}
void Greeter::greet() const
{
std::cout << "Hello, " << whom << std::endl;
}
public ref class Greeter
{
public:
Greeter(String^ whom);
void greet();
private:
initonly String^ whom;
};
int main()
{
(gcnew Greeter(L"world"))->greet();
}
Greeter::Greeter(String^ whom) : whom(whom) {}
void Greeter::greet()
{
Console::WriteLine(L"Hello, {0}", whom);
}
{
public:
Greeter(String^ whom);
void greet();
private:
initonly String^ whom;
};
int main()
{
(gcnew Greeter(L"world"))->greet();
}
Greeter::Greeter(String^ whom) : whom(whom) {}
void Greeter::greet()
{
Console::WriteLine(L"Hello, {0}", whom);
}
erlang
Greeter = make_greeter("world!"),
Greeter(greet).
Greeter(greet).
Instantiate object with mutable state
Reimplement the Greeter class so that the
For example, if the greetee is changed to
Hello, Tommy!
The getter would then be used to display the line:
I have just greeted Tommy.
'whom' property or data member remains private but is mutable, and is provided with getter and setter methods. Invoke the setter to change the greetee, invoke 'greet', then use the getter in displaying the line, "I have just greeted {whom}.".
For example, if the greetee is changed to
'Tommy' using the setter, the 'greet' method would display:
Hello, Tommy!
The getter would then be used to display the line:
I have just greeted Tommy.
ruby
class Greeter
attr_accessor :whom
def initialize(whom) @whom = whom end
def greet() puts "Hello, #{@whom}!" end
end
greeter = Greeter.new("world") ; greeter.greet()
greeter.whom = 'Tommy' ; greeter.greet()
puts "I have just greeted %s" % greeter.whom
attr_accessor :whom
def initialize(whom) @whom = whom end
def greet() puts "Hello, #{@whom}!" end
end
greeter = Greeter.new("world") ; greeter.greet()
greeter.whom = 'Tommy' ; greeter.greet()
puts "I have just greeted %s" % greeter.whom
csharp
class Greeter
{
public string Name {get;set;}
public void Greet(){
Console.WriteLine("Hello, {0}",Name);
}
public Greeter(string name){
this.Name = name;
}
// Driver
public static void Main()
{
var g = new Greeter("Dante");
g.Name = "Tommy";
g.Greet();
Console.Write("I have just greated {0}", g.Name);
}
}
{
public string Name {get;set;}
public void Greet(){
Console.WriteLine("Hello, {0}",Name);
}
public Greeter(string name){
this.Name = name;
}
// Driver
public static void Main()
{
var g = new Greeter("Dante");
g.Name = "Tommy";
g.Greet();
Console.Write("I have just greated {0}", g.Name);
}
}
cpp
#include <iostream>
using namespace std;
class Greeter {
string whom_;
public:
Greeter(const string &whom) : whom_(whom) {}
string get_whom() const {
return whom_;
}
void set_whom(const string &whom) {
whom_ = whom;
}
void greet() const {
cout << "Hello " << whom_ << "!" << endl;
}
};
int main()
{
Greeter greeter("world");
greeter.greet();
greeter.set_whom("Tommy");
greeter.greet();
cout << "I have just greeted " + greeter.get_whom() << "." << endl;
}
using namespace std;
class Greeter {
string whom_;
public:
Greeter(const string &whom) : whom_(whom) {}
string get_whom() const {
return whom_;
}
void set_whom(const string &whom) {
whom_ = whom;
}
void greet() const {
cout << "Hello " << whom_ << "!" << endl;
}
};
int main()
{
Greeter greeter("world");
greeter.greet();
greeter.set_whom("Tommy");
greeter.greet();
cout << "I have just greeted " + greeter.get_whom() << "." << endl;
}
Implement Inheritance Heirarchy
Implement a Shape abstract class which will form the base of an inheritance hierarchy that models 2D geometric shapes. It will have:
* A non-mutable
* A
* A
* A non-mutable
'name' property or data member set by derived or descendant classes at construction time
* A
'area' method intended to be overridden by derived or descendant classes ( double precision floating point return value)
* A
'print' method (also for overriding) will display the shape's name, area, and all shape-specific values
Two derived or descendant classes will be created:
* Circle -> Constructor requires a 'radius' argument, and a 'circumference' method to be implemented
* Rectangle -> Constructor requires 'length' and 'breadth' arguments, and a 'perimeter' method to be implemented
Instantiate an object of each class, and invoke each objects 'print' method to show relevant details.
ruby
class Shape
def initialize(name="") @name = name end
end
class Circle < Shape
def initialize(radius) super("circle") ; @radius = radius end
def area() 3.14159 * @radius * @radius end
def circumference() 2 * 3.14159 * @radius end
def print()
puts "I am a #{@name} with ->"
puts "Radius: %.2f" % @radius
puts "Area: %.2f" % self.area()
puts "Circumference: %.2f\n" % self.circumference()
end
end
class Rectangle < Shape
def initialize(length, breadth) super("rectangle") ; @length = length ; @breadth = breadth end
def area() @length * @breadth end
def perimeter() 2 * @length + 2 * @breadth end
def print()
puts "I am a #{@name} with ->"
printf("Length, Width: %.2f, %.2f\n", @length, @breadth)
puts "Area: %.2f" % self.area()
puts "Perimeter: %.2f\n" % self.perimeter()
end
end
# ------
shapes = [Circle.new(4.2), Rectangle.new(2.7, 3.1), Rectangle.new(6.2, 2.6), Circle.new(17.3)]
shapes.each {|shape| shape.print}
def initialize(name="") @name = name end
end
class Circle < Shape
def initialize(radius) super("circle") ; @radius = radius end
def area() 3.14159 * @radius * @radius end
def circumference() 2 * 3.14159 * @radius end
def print()
puts "I am a #{@name} with ->"
puts "Radius: %.2f" % @radius
puts "Area: %.2f" % self.area()
puts "Circumference: %.2f\n" % self.circumference()
end
end
class Rectangle < Shape
def initialize(length, breadth) super("rectangle") ; @length = length ; @breadth = breadth end
def area() @length * @breadth end
def perimeter() 2 * @length + 2 * @breadth end
def print()
puts "I am a #{@name} with ->"
printf("Length, Width: %.2f, %.2f\n", @length, @breadth)
puts "Area: %.2f" % self.area()
puts "Perimeter: %.2f\n" % self.perimeter()
end
end
# ------
shapes = [Circle.new(4.2), Rectangle.new(2.7, 3.1), Rectangle.new(6.2, 2.6), Circle.new(17.3)]
shapes.each {|shape| shape.print}
csharp
// While abstract classes do exist in C#, it is most common to use
// an interface in this type of situation.
// It is a common idiom to prefix interface names with an I
public interface IShape {
string Name { get; }
double Area { get; }
void Print();
}
public class Circle : IShape {
private double Radius { get; set; }
public Circle(double radius) {
Name = "Circle";
Radius = radius;
}
public string Name { get; private set; }
public double Area {
get {
return Math.PI * Radius * Radius;
}
}
public double Circumference {
get {
return Math.PI * (Radius + Radius);
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Circumference: {2}\n Radius: {3}",
this.Name,
this.Area,
this.Circumference,
this.Radius
);
}
}
public class Rectangle : IShape {
private double Length { get; set; }
private double Breadth { get; set; }
public Rectangle(double length, double breadth) {
Name = "Rectangle";
Length = length;
Breadth = breadth;
}
public string Name { get; private set; }
public double Area {
get {
return Length * Breadth;
}
}
public double Perimeter {
get {
return (Length * 2) + (Breadth * 2 );
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Perimeter: {2}\n Length: {3}\n Breadth: {4}",
this.Name,
this.Area,
this.Perimeter,
this.Length,
this.Breadth
);
}
}
// Driver
public class InheritanceHeirarchy {
public static void _Main() {
var c = new Circle(2.1);
c.Print();
Console.WriteLine();
var r = new Rectangle(2.2, 3.3);
r.Print();
}
}
// an interface in this type of situation.
// It is a common idiom to prefix interface names with an I
public interface IShape {
string Name { get; }
double Area { get; }
void Print();
}
public class Circle : IShape {
private double Radius { get; set; }
public Circle(double radius) {
Name = "Circle";
Radius = radius;
}
public string Name { get; private set; }
public double Area {
get {
return Math.PI * Radius * Radius;
}
}
public double Circumference {
get {
return Math.PI * (Radius + Radius);
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Circumference: {2}\n Radius: {3}",
this.Name,
this.Area,
this.Circumference,
this.Radius
);
}
}
public class Rectangle : IShape {
private double Length { get; set; }
private double Breadth { get; set; }
public Rectangle(double length, double breadth) {
Name = "Rectangle";
Length = length;
Breadth = breadth;
}
public string Name { get; private set; }
public double Area {
get {
return Length * Breadth;
}
}
public double Perimeter {
get {
return (Length * 2) + (Breadth * 2 );
}
}
public void Print() {
Console.WriteLine( " Name: {0}\n Area: {1}\n Perimeter: {2}\n Length: {3}\n Breadth: {4}",
this.Name,
this.Area,
this.Perimeter,
this.Length,
this.Breadth
);
}
}
// Driver
public class InheritanceHeirarchy {
public static void _Main() {
var c = new Circle(2.1);
c.Print();
Console.WriteLine();
var r = new Rectangle(2.2, 3.3);
r.Print();
}
}
cpp
#include <string>
#include <iostream>
using namespace std;
static const double PI = 3.141592;
class Shape {
protected:
string name_;
public:
Shape(const string& name) : name_(name) { }
virtual double area() const = 0;
virtual void print() const = 0;
};
class Circle : public Shape {
double radius_;
public:
Circle(double radius) : Shape("circle"), radius_(radius) { }
double area() const {
return PI * radius_ * radius_;
}
void print() const {
cout << "A " << name_ << " with radius " << radius_ << ", area "
<< area() << " and circumference " << circumference() << "."
<< endl;
}
double circumference() const {
return 2 * PI * radius_;
}
};
class Rectangle : public Shape {
double length_;
double breadth_;
public:
Rectangle(double length, double breadth) :
Shape("rectangle"), length_(length), breadth_(breadth) { }
double area() const {
return length_ * breadth_;
}
void print() const {
cout << "A " << name_ << " with length " << length_ << ", breadth "
<< breadth_ << ", area " << area() << " and perimeter "
<< perimeter() << "." << endl;
}
double perimeter() const {
return 2 * length_ + 2 * breadth_;
}
};
int main(int argc, char *argv[])
{
Circle circle(4);
circle.print();
Rectangle rectangle(2, 5.5);
rectangle.print();
}
#include <iostream>
using namespace std;
static const double PI = 3.141592;
class Shape {
protected:
string name_;
public:
Shape(const string& name) : name_(name) { }
virtual double area() const = 0;
virtual void print() const = 0;
};
class Circle : public Shape {
double radius_;
public:
Circle(double radius) : Shape("circle"), radius_(radius) { }
double area() const {
return PI * radius_ * radius_;
}
void print() const {
cout << "A " << name_ << " with radius " << radius_ << ", area "
<< area() << " and circumference " << circumference() << "."
<< endl;
}
double circumference() const {
return 2 * PI * radius_;
}
};
class Rectangle : public Shape {
double length_;
double breadth_;
public:
Rectangle(double length, double breadth) :
Shape("rectangle"), length_(length), breadth_(breadth) { }
double area() const {
return length_ * breadth_;
}
void print() const {
cout << "A " << name_ << " with length " << length_ << ", breadth "
<< breadth_ << ", area " << area() << " and perimeter "
<< perimeter() << "." << endl;
}
double perimeter() const {
return 2 * length_ + 2 * breadth_;
}
};
int main(int argc, char *argv[])
{
Circle circle(4);
circle.print();
Rectangle rectangle(2, 5.5);
rectangle.print();
}
Implement and use an Interface
Create a Serializable interface consisting of
* Accept a stream or handle or descriptor argument for the source or destination
* Save to destination or restore from source the properties or data members of the implementing class (restrict yourself to the primitive types
Next, create a Person class which has
'save' and 'restore' methods, each of which:
* Accept a stream or handle or descriptor argument for the source or destination
* Save to destination or restore from source the properties or data members of the implementing class (restrict yourself to the primitive types
'int' and 'string')
Next, create a Person class which has
'name' and 'age' properties or data members and implements this interface. Instantiate a Person object, save it to a serial stream, and instantiate a new Person object by restoring it from the serial stream.
ruby
class Person
def initialize(name, age)
@name, @age = name, age
end
end
tom = Person.new("Tom Bones", 23)
File.open('tommy.dump', 'w+') {|f| f.write(Marshal.dump(tommy)) }
toms_clone = Marshal.load(File.read('tommy.dump'))
def initialize(name, age)
@name, @age = name, age
end
end
tom = Person.new("Tom Bones", 23)
File.open('tommy.dump', 'w+') {|f| f.write(Marshal.dump(tommy)) }
toms_clone = Marshal.load(File.read('tommy.dump'))
cpp
struct person
{
person(){}
person(const string &name, int age) : name_(name), age_(age) {}
string name_;
int age_;
template<typename Archive>
void serialize(Archive &ar, const unsigned int version) {
ar & name_ & age_;
}
};
int main()
{
const char *fn = "filename.txt";
person k("Ken", 38);
{
ofstream ofs(fn);
archive::text_oarchive oa(ofs);
oa << k;
}
person restored_person;
{
ifstream ifs(fn);
archive::text_iarchive ia(ifs);
ia >> restored_person;
}
cout << "Name : " << restored_person.name_ << endl
<< "Age : " << restored_person.age_ << endl;
}
{
person(){}
person(const string &name, int age) : name_(name), age_(age) {}
string name_;
int age_;
template<typename Archive>
void serialize(Archive &ar, const unsigned int version) {
ar & name_ & age_;
}
};
int main()
{
const char *fn = "filename.txt";
person k("Ken", 38);
{
ofstream ofs(fn);
archive::text_oarchive oa(ofs);
oa << k;
}
person restored_person;
{
ifstream ifs(fn);
archive::text_iarchive ia(ifs);
ia >> restored_person;
}
cout << "Name : " << restored_person.name_ << endl
<< "Age : " << restored_person.age_ << endl;
}
