Classes-Enterprise Applicatio Development-Lecture Handout, Exercises of Software Development

This lecture handout was provided by Sir Aabher Dutt at Indian Institute of Space Science and Technology for Enterprise Application Development course. It includes: Class, Syntax, Objects, Instances, Member, Functions, Constructors, Destructors, Data, Types

Typology: Exercises

2011/2012

Uploaded on 07/19/2012

pankti
pankti 🇮🇳

4.1

(8)

39 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
iv
Table of Contents
1 Introduction .........................................................................................................................1
2 Class ...................................................................................................................................1
2.1 Class Syntax .................................................................................................................1
3 Public and Private................................................................................................................2
3.1 Member Functions ........................................................................................................2
4 Object: ................................................................................................................................3
4.1 Objects and Instances ....................................................................................................3
4.2 Use of Class with Objects .............................................................................................4
4.2.1 Calling Member Functions .....................................................................................4
4.2.2 Objects as Physical Objects ....................................................................................5
4.2.3 Objects as Data Types ............................................................................................5
5 Constructors and Destructors ...............................................................................................6
6 Member Functions outside the Class ....................................................................................7
6.1 Const member function .................................................................................................7
6.2 Constant Object ............................................................................................................8
7 Implemented Class ..............................................................................................................8
8 Summary .............................................................................................................................9
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Classes-Enterprise Applicatio Development-Lecture Handout and more Exercises Software Development in PDF only on Docsity!

iv

  • 1 Introduction ......................................................................................................................... Table of Contents
  • 2 Class ...................................................................................................................................
    • 2.1 Class Syntax .................................................................................................................
  • 3 Public and Private................................................................................................................
    • 3.1 Member Functions ........................................................................................................
  • 4 Object: ................................................................................................................................
    • 4.1 Objects and Instances ....................................................................................................
    • 4.2 Use of Class with Objects .............................................................................................
      • 4.2.1 Calling Member Functions .....................................................................................
      • 4.2.2 Objects as Physical Objects ....................................................................................
      • 4.2.3 Objects as Data Types ............................................................................................
  • 5 Constructors and Destructors ...............................................................................................
  • 6 Member Functions outside the Class....................................................................................
    • 6.1 Const member function .................................................................................................
    • 6.2 Constant Object ............................................................................................................
  • 7 Implemented Class ..............................................................................................................
  • 8 Summary .............................................................................................................................

1 Introduction

“Objects in the real world have only one thing in common -- they are all different.”

Central concepts behind object oriented programming – classes, objects and classification are best part for computer programmers. The earliest characterization of class verses instances was given by Plato over two thousand years ago. His work was followed by his student Aristotle. Aristotle's work on classification did not receive much criticism for a long time. His "classical" view was first challenged in the 19th century by the famous British philosophers W. Whewell and W.S. Jevons. They emphasized that there are no universal rules to determine what properties to use as a basis of classification of objects. Criticism on classification was continued later in our century by Ludwig Wittgenstein. Then with the passage of time they got prototype theory which is base of modern object oriented programming.

2 Class

Class is like an array; it is a derived type whose elements have other types. But unlike an array, elements of class may have different types. Furthermore, some elements of class may be functions including operators.

2.1 Class Syntax

Class employee {

public:

void set_age();

void show_age ();

private:

4 Object:

An object is a component of a program that knows how to perform certain actions and to interact with other pieces of the program. A class is a type, and an object of this class is just a variable. Let's say that we are writing a text-based video game. Our video game will have two types of characters: the players and the monsters. A player has to know the values of certain attributes: health, strength, and agility. A player must also know what type of weapon and what type of armor they possess. A player must be able to move through a maze, attack a monster, and pick up treasure. So, to design this "player object", we must first separate data that the player object must know from actions that the player must know how to execute. The definition for a player object could be:

Player Object: data: health strength agility type of weapon type of armor actions: move attack monster get treasure END;

4.1 Objects and Instances

There is a very important distinction between an object and an instance of an object. An object is actually a definition, or a template for instances of that object. An instance of an object is an actual thing that can be manipulated. For instance, we could define a Person object, which may include such member data as hair color, eye color, height, weight, etc. An instance of this object

could be "mubashar" and mubashar has values for hair color, eye color, etc. This allows for multiple instances of an object to be created.

4.2 Use of Class with Objects

Classes are used with the help of objects which I have already defined. Now we discuss how exactly we use a class.

Employee e1,e2;

Above expression defines two objects of class Employee. These were not created at the moment when class was declared. Defining an object is similar to defining a variable of any data type.

Defining an object in this way is called creating them. It is also called instantiating them.

4.2.1 Calling Member Functions

The next two statements call the member functions in main.

e1.set_age(20);

e2.set_age(25);

These statements do not look like normal functions call. Instead object names are connected with their name. This strange syntax is used to call a member function that is associated with a specific object. Because set_age is a member function of class Employee, it must always be called in connection of with an object of this class. It cannot be written as

set_age(20) ;

On doing this, compiler will issue an error because member functions can be accessed with the help of objects of that class.

}; //End of class

int main(){

Distance d1,d2;

d1.setdist(11,6.5);

d2.getdist();

cout<<”Distance1=”<< d1.show_dist();

cout<<”Distance2=”<< d2.show_dist();

cout<<endl;

getch();

}

Above class has two data members and three functions. set_dist use arguments to set the values of inches and feet. get_dist gets the values from user through key strokes. And show_dist shows the values of feet and inches.

Here value of objects of the class can be set in two ways. set_dist sets the value of distance through arguments whereas second uses user given value.

5 Constructors and Destructors

At the time of creation, object does not have any value. But OOP provide a mechanism for object to have some initial value through constructors. This is called automatic initialization. A

constructor is a member function that is automatically executed whenever object is formed. Similarly at the end of the program object needs to be destroyed which is done through destructors.

A constructor has same name as class and a destructor has same name as constructor but it is preceded by a tilde.

6 Member Functions outside the Class

Member functions can be defined outside the class. It is declared inside the class. To define a member function outside the class, syntax is somewhat unfamiliar. In this syntax function name is preceded by class name and scope resolution operator (two colon ::).

6.1 Const member function

A constant member function guaranties that it will never modify any of its class’s member data.

class aClass{

private:

int alpha;

public:

void acons_funcrtion(){

alpha=99;}

void anoncons_func() const{

alpha=99; // Not Allowed

}

width = new int ; height = new int ; *width = a; *height = b; }

CRectangle::~CRectangle () { delete width; delete height; }

main () { CRectangle rect (3,4), rectb (5,6); cout << "rect area: " << rect.area() << endl; cout << "rectb area: " << rectb.area() << endl; getch() ; }

8 Summary

A class is a specification or blue print for a number of objects. Objects consist of both data and functions that operate on data. In a class declaration, data and functions can be made private meaning they can be accessed only by the member function of the class or public meaning that they can be accessed all over in the program.

A member function is a function that is member of a class and it has an access to private data members as well.

A constructor is a member function having same name as that of the class and it is executed every time an object is formed. It has no return time but has arguments.

A destructor is called whenever an object is destroyed.

One reason to use OOP is real world correspondence between real world objects and OOP classes. Deciding what objects and classes to use in a program can be complicated. For small programs trial and errors may be sufficient but for large programs more systematic approach is needed which is provided by OOP that basically depends upon classes.