Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pascal classes example 2
05-02-2011, 12:33 PM (This post was last modified: 05-02-2011 01:24 PM by codecaine.)
Post: #1
Pascal classes example 2
Below is my second class example. If you did not see my first class example go check it out before trying to start here. Here you will learn how to derive a class, overload methods, and the usage of the virtual keyword.

My next class example should be very short. It will display how to create a interface class and abstract class. I did not want to add to much in this example.

In my example I have all the classes including the main program in one file. You usually want to put classes in their own file so it don't get to clutter. I merely did it this way so user can see everything at once.

Code:
{
    About: This is my second class example. It displays how to create a class deriving from another class, overloading functions, and the use of a virtual function
    Author: Jerome Scott II aka codecaine
    Compiled: Compiled with the free pascal compiler on  1 May 2011 @ 9:02PM
}

program InheritanceExample(input,output);

{$MODE OBJFPC} //directive to enable use of creating classes
{$M+} //directive that allows class constructors and destructors

{ Creating a base class here}
Type
    TStudent = Class //base class for student
    private
        studentName : String; //holds student name but is only accessible directly by this class
    public
        constructor Create(sName : String); //Default constructor that initialize the student name
        procedure setStudentName(sName : String); //sets a student name
        function getStudentName() : String; //retrieve student name
        procedure display(); virtual; //prints the student name on the string. Notice that I have virtual at the end. This allows the function to be overriding in a derived class as you will see in the next class
    property name : String read getStudentName write setStudentName; //allow access to the set and get methods as a property with the property name name
end;

{ Creating a dervied class here }
Type
    TInstructor = Class(TStudent) //creating Instructor class that derives from the student class
    private
        instructorName : String; //holds student name but is only accessible directly by this class
    public
        { If you ever had multiple functions with the same name you know you use the overload operater so the compiler can reconginize it }
        constructor Create(iName : String); overload; //default constructor with not student giving
        constructor Create(iName : String; sName : String); overload; //default constructor with a student argument to give the student class a student name
        procedure setInstructorName(iName : String); //sets the instructor name
        function getInstructorName() : String; //returns the instructor name
        procedure display(); override; //Since student display procedure was virtual not I can override it and change it for the instructor using override. If a class was to inherit from TInstuctory they cannot override it unless they declare it virtual as well
end;

{
    I want to make these examples as small as possible, so there won't be much checking if a string is empty and other cases like that. The goal is for you to understand how inheritance can be use.
    If you are programming classes in Pascal I can assume you know the basics of error checking, etc....
}


{
    Below are the methods of the TStudent class. Remember when using classes you must use the format of function or procedure followed by the class name dos operator and method name
}

//default constructor to give student name a value.
constructor TStudent.Create(sName : String);
begin
    studentName := sName; //user could put a empty string there is no error checking. So don't bludgeon me with smart comments!
end;

//set the student name
procedure TStudent.setStudentName(sName : String);
begin
    studentName := sName;
end;

//return the stuended name
function TStudent.getStudentName() : String;
begin
    result := studentName;
end;

//display who the student is
procedure TStudent.display();
begin
    writeLn('I am ', studentName);
end;


{
    Methods for TInstructor class
    If you did not see my first class inherited allows you to access a base class. In you are using inherited in the constructor it should be at the very top being decalred, and
    if your are in the destructor using inherited it should be the last call you do.
}

//instructor to create the instructor class with not student name
constructor TInstructor.Create(iName : String);
begin
    inherited Create(''); //initialize the student class with no name
    instructorName := iName; //give instructor a name from iName
end;

//instructor to create the instructor class with a student name giving for the base class TStudent
constructor TInstructor.Create(iName : String; sName : String); //default construct with a student argument to give the student class a student name
begin
    inherited Create(sName); //initialize the base class which is the TStudent class constructor
    instructorName := iName; //initialize instructor from iName
    
end;

procedure TInstructor.setInstructorName(iName : String); //sets the instructor name
begin
    instructorName := iName;        
end;

//returns the instructor name
function TInstructor.getInstructorName() : String;
begin
    result := instructorName;
end;

//Since student display procedure was virtual not I can override it and change it for the instructor using override. If a class was to inherit from TInstuctory they cannot override it unless they declare it virtual as well
procedure TInstructor.display();
begin
    writeLn('I am your instructor ', getInstructorName()); //display who the instructor is
end;

var
    student : TStudent; //variable to use student class
    instructor : TInstructor; //varible to use instructor class
{
    The body of the program starts here to demostrate the class

}    
begin
    try //try to initialize a student class
        student := TStudent.Create('codecaine'); //initialize TStudent class for student
        student.display(); //display who the student is
        { Notice that I am using the property to set and get the student name, but you can also use setStudentName and getStudentName. Most people prefer using properties for quick access and it still executes those methods! }
        student.name := 'Jerome Scott II'; //change student from codecaine to Jerome Scott II"
        writeLn('You changed your name to ', student.name);
    finally //if there was a error and student wasn't free from memory free it
        if student <> nil then
            student.free(); //free student from memory
    end;
    
{ Example of using the the Instructor class }
    try
        instructor := TInstructor.Create('Money Mike');
        instructor.display(); //display the instructor name
        instructor.studentName := 'Jerome'; //instructor class calling a TStudent class to set a student name using the property name;
        {
            Display the instructor saying hi to the student. Notice how useful the properties are in classes instead of using get in set methods?
            Since I did not do any properties for the instructor class I have to call the individual function and procedure to access his name
        }
        writeLn('Hi ', instructor.studentName, ', I am ', instructor.getInstructorName(), ' your instructor!');
        
        { freeing instrutor class so I can show a example with it again using its other constructor. }
        
        //Check if the instructor is initialized
        if instructor <> nil then
            instructor.free(); //free the memory for reusage
            
        instructor := TInstructor.Create('Timmy Tim', 'code'); //re-declare the class to show a example of the constructor with two arguments
        writeLn('Hi ', instructor.studentName, ', I am ', instructor.getInstructorName(), ' your instructor!'); //display the new instructor class student and instructor names
    finally
        //If the instructor is still holding memory free it
        if instructor <> nil then
            instructor.free();
    end;

    
end.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: