Classes and Objects, C++ Assignment Help - C++ Tutor Service

Assignment Help >>  Classes and Objects

CLASS AND OBJECTS

1-INTRODUCTION

Programming in java (like other object-oriented languages) is based around the use of objects and classes .Objects and classes are the basic building blocks of an object oriented computer program. Object-Oriented programming (OOP) is a programming methodology that uses object and class and is used to implement real world scenarios.

OBJECT:

According to OOP, every real world entity is an object which consists of state and behavior.

Information about the object is known as its state and things that an object do is known as its behavior (or functionality). For example, according to oop a pen is also an object where pen name , its company name denotes its state and pen is used to write denotes its behavior (i.e. functionality).

CLASS:

Class is a concept that comes from word classification which means objects that have similar types of state and behavior can be grouped into a category known as class.

Programmatically, class is a powerful user-defined data type that is used to bind data and functions together. Class provides the implementation manner of OOP because using class we can implement all the features of OOP.

DECLARING A CLASS

Here class keyword tells the compiler that ClassName is a class. Pair of curly braces defines the body of the class. A class is a collection of data and methods. The methods act on data of the class. Data and methods are collectively called as the members of a class. Let us now see how the methods and data are created inside a class and how are they used.

class ClassName{

.......Data Members......

.......Methods.................

}

2-BENEFITS OF OBJECT-ORIENTED PROGRAMMING

Object Oriented Programming has more merits over other programming styles or ways:

Reusing and Recycling of code: Objects created for Object Oriented Programs can easily be reused in other programs by the use of concept of inheritance, using objects of classes and many more.

Encapsulation (part 1): Once an Object is created, knowledge of its use is not necessary for use of it. In older time while programming, coders needed to understand the details of a piece of code before using it (in same or another program).

Encapsulation (part 2): Objects have the property to hide certain parts of themselves from programmers. This prevents programmers from tampering with values they shouldn't. Moreover, the object controls how one interacts with it, preventing other kinds of errors. For example, a programmer (or another program) cannot set the width of a window to -400.

Advantageous designing: Large programs are bit typical to write. Object Oriented Programs force designers to go through an immense planning period, which makes for better designs with less faults. In addition, once a program reaches a certain size, Object Oriented Programs are actually easier to program than non-Object Oriented ones.

Software Maintenance: Programs are not disposable. Legacy code must be dealt with on a daily basis, either to be ameliorate upon (for a new version of an exist piece of software) or made to work with newer computers and software. An Object Oriented Program is much easier to modify and sustain than a non-Object Oriented Program. So although a lot of work is spent before the program is written, less work is needed to maintain it over time.

3-BASIC CONCEPTS

What is an object?

Object is an identifiable entity with some characteristics and behavior.

While programming using OOP approach ,the characteristics of an object is represented by its data and its behavior is represented by its functions or methods associated. Therefore ,in OOP programming object represents an entity that can store data and has its interface through functions/methods.

An object oriented program consists of many objects, each having its own characteristics and behavior. Recall that an object has certain details hidden while some of these are exposed due to the property of data abstraction. For example, in a car object, only steering, gear box, control panel etc... are exposed while rest of the details such as motor, petrol tank, etc.. are hidden.

FEATURES OF OBJECTS

Object has following four key-features:

1-Characteristics/attributes: These are the properties that characterize an object.

2-Behaviour/methods: These are the functionalities associated with an object.

3-Identity: The unique way of referring an object.

4-State: It depicts how the object has reacted to the method applied to it.

What is a class?

A class is a blueprint that defines similar type of objects.

A class definition identifies all the parameters that define an object of that particular class. Using this class definition, objects are created. So we can say an object is an instance of a class.

Relationship between a class and an object can be defined as: a class broadly represents characteristics and behavior of similar types of object i.e., is a blueprint whereas an object is the actual instance created from the blueprint.

4-STRUCTURES

Ordinary variables can hold one piece of information and how arrays can hold a number of pieces of information of the same data type. These two data types can handle a great variety of situations. But quite often we deal with entities that are collection of dissimilar data types, so we use structures. A structure contains a number of data types grouped together. These data types may or may not be of same data type.

A structure is a composite data type that defines physically grouped variables to be placed under one name in a block of memory allowing different variables to be accessed.

4.1-Declaration of structures in C

struct book

{

char name;

float price;

int pages;

};

This statement defines a new data type called struct book. Each variable of this data type will consist of a character variable called name, a float variable called price and an integer variable called pages. The general form in which we can declare a structure is given below:

struct <structurename>

{

Structure element 1;

Structure element 2;

Structure element 3;

...........

...........

};

Once the new structure data type has been defined, one or more variables can be declared to be of that type. For example, the variables b1, b2, b3 are being declared to be of the type struct book, as,

struct book b1, b2, b3;

This statement sets aside space in memory. It makes available space to hold all the elements in the structure.

4.2- DECLARATION OF STRUCTURES IN C++

To define a structure in C++ you must use the same struct statement as that in C. The struct statement defines a new data type, with more than one member, for your program. The format of struct statement is:

struct <structureTag>

{

Member definition 1;

Member definition 2

.............

...........

};

5-DECLARATION OF CLASS IN C++

Classes and structs are user- defined types, defined by the class specifier. When we define any class, we are not defining any data, we just define a structure or a blueprint, as to what the object of that class type will contain and what operationscan be performed on that object.

Below is the syntax for defining the class,

class ClassName

{

Access specifier;

Data members;

Member functions(){}

};

   5.1-BASIC GUIDELINES

The following guidelines should be kept in mind before declaring a class:

ð  Each module with its .h and .cpp file should correspond to a clear piece of functionality.

ð  Always use include guards in a header file.

ð  All of the declaration needed to use a module must appear in its header file, and this file is always used to access the module.

ð  No namespace using statements are allowed at the top level in a header file.

ð  The header files contain only declarations, templates, and inline function definitions, and are included by the .cpp file for the module.

6-DEFINING MEMBER FUNCTIONS OF A CLASS

A member function of a class is that function which has its own definition and its prototype within the class definition. It operates on any object of the class of which it is a member, and has access to all the members of a class for that object. A member function can be defined using the following syntax:

class <className>

{

returnType FunctionName(parameters)

{

..........statement 1..........

..........statement 2...........

...........................................

}

}

6.1-INSIDE A CLASS

 A member function of a class can also be defined inside the class. However, when a member function is defined inside the class, the class name and the scope resolution operator are not specified in the function header. The member functions which are defined inside a class definition are by default inline functions.

6.2-OUTSIDE A CLASS

Defining a member function outside a class requires the function declaration (function prototype) to be provided inside the class definition. The member function is declared inside the class like a normal function. This declaration informs the compiler that the function is a member of the class and that it has been defined outside the class. After a member function is declared inside a class, it should be defined in the program.

7-DEFINING OBJECT OF A CLASS

As we know that a class is a blueprint for object, so we can say that an object can be created from a class. We declare the object of the class in the same manner as that variable declaration of basic type. Following example declare objects of some class box.

box box1;

box box2;

Here box1 and box2 are objects of class box.

8-INVOKING MEMBER FUNCTIONS OF A CLASS

WHEN YOU INSTANTIATE AN OBJECT USING THE NEW KEYBOARD, MEMORY IS ALLOCATED FOR EACH OF THE FIELDS AND METHODS IN THE CLASS. YOU NEED A REFERENCE TO THE OBJECT TO ACCESS THESE FIELDS AND METHODS USING DOT OPERATOR.

For example, the following statements declare an employee object and change the name field of the object:

employee emp1=new employee();

emp1.paySlip();

9-THE KEYWORDS PUBLIC, PRIVATE AND PROTECTED

PUBLIC- public members and variables are visible to all classes. Purpose - Methods and variables of interest to user of the class.

PRIVATE- private members and variables are visible only within the same class, not by inheritors, not by other classes in the package. Purpose : Variables and methods that should not be changed by someone extending the class depend on them working accurately as written.

PROTECTED- protected classes and variables are visible to classes outside the package that inherits the class, also to all classes in a package. Purpose: Methods and variables of interests to third parties who may extend your class.

10-CHARACTERISTICS OF PRIVATE MEMBER OF A CLASS

Private variables or methods can be accessed only from the methods of the class to which it belongs. A subclass also does not have access to a private variable.

ð  Private member is only accessible inside the same class.

ð  Private members are not accessible within the subclass inside the same package.

ð  Private members are not accessible outside the package.

ð  Private members are not accessible within the subclass outside the package.

Syntax of private member is:

private data type variable;

11-ARRAY OF OBJECTS

Array of a type class can also be created same as that of some user defined data type. The array of type class contains the objects as its individual elements. Thus an array of type class is known as array of objects. Syntax to define an array of objects is:

Class_name array_name[size_of_array];

12-STATIC DATA MEMBERS

A data members for a class that is not created for the instances of the class rather one sharable copy of it is maintained, is called class variable/ static variable.

The keyword static means only one copy exists for the entire class irrespective of the number of objects that exist for that class. Memory for the static members is created before the object is created, because of this they can be called with the class name.

13-STATIC MEMBER FUNCTIONS

A static member function is also created by placing keyword static before their declaration. Static method uses no instance variables of any object of the class they are defined in. they can use class variables of the class without using any object name. they use the class name instead for their invocation.

Static members can be created as the following syntax:

static return_type function_name(<parameters>)

{

}

Static member functions/ data members of a class are visible and shared by all instances of the class.

14-PASSING OBJECTS AS ARGUMENTS

The objects of a class can be passed as quarrel to member functions as well as those functions which are not a member either by value or by reference. Whenever an object of a class is passed to a member function of the same class, its data members can be retrieved inside the function using the object name and the dot operator. Regardless of how, the data members of the calling object can be directly controlled inside the function without using the object name and the dot operator.

To understand how objects are passed and accessed within a member function, consider this example.

int main ()

{

weight wl,w2 ,w3;

cout<<"Enter weight in kilograms and grams\n";

cout<<"\n Enter weight #1" ;

wl.getdata();

cout<<" \n Enter weight #2" ;

w2.getdata();

w3.sum_weight(wl,w2);

cout<<"/n Weight #1 = ";

wl.putdata();

cout<<"Weight #2 = ";

w2.putdata();

cout<<"Total Weight = ";

w3.putdata();

return 0;

}

15-FRIEND FUNCTIONS

A friend function of a class can be defined outside that class scope but it has the right to handle all private and protected members of the class. Though the prototypes for friend functions appear in the class definition yet friends cannot be included in member functions.

  15.1-DECLARATION OF A FRIEND FUNCTION

To declare a friend function of a class, start the function prototype in the class definition with keyword friend as follows:

class Box {

   double width;

public:

   double length;

   friend void printWidth( Box box );

   void setWidth( double wid );

};

  15.2-INVOKING A FRIEND FUNCTION

Consider the following program:

#include <iostream>

using namespace std;

class Box {

   double width;

public:

   friend void printWidth( Box box );

   void setWidth( double wid );

};

// Member function definition

void Box::setWidth( double wid ) {

   width = wid;

}

// Note: printWidth() is not a member function of any class.

void printWidth( Box box ) {

   /* Because printWidth() is a friend of Box, it can

   directly access any member of this class */

   cout << "Width of box : " << box.width <<endl;

}

// Main function for the program

int main( ) {

   Box box;

   // set box width with member function

   box.setWidth(10.0);

 // Use friend function to print the width.

   printWidth( box );

   return 0;

}

On compiling and executing the above program, it produces the final result:

Width of box : 10


Expertsminds.com - Classes and Objects Assignment Help, Classes and Objects Assignment Writing Help, Classes and Objects Assignment Tutors, Classes and Objects Solutions, Classes and Objects Answers, C++ Tutorial Assignment Experts Online



Most Matched Writing Services:-
Captcha

Get Academic Excellence with Best Skilled Tutor! Order Assignment Now! Submit Assignment