You are on page 1of 33

Presented by :

Md. Asad Hossain Roll no. : 0903087 Sec. B , Group : B1 Dept. of EEE, KUET

To study on Object-Oriented Programming To study on Basic concepts of Object-Oriented Programming To study on Member function To study on Inline functions To study on Friendly Functions To study on CONSTRUCTORS AND DESTRUCTORS To study on OPERATOR OVERLOADING To study on INHERITANCE To study on POINTERS To study on Working with files

Object oriented programming is an approach to program organization and development that attempts to eliminate some of the pitfalls of conventional programming methods.

C++ :
C++ is an object oriented programming language. It was developed at AT&T Bell Laboratories in Murray Hill,New Jersey, USA,in the early 1980s. In November 1997, the ANSI standards committee standardised these changes and added new features to the language specifications. C++ is a superset of C. Most of what we already know about C applies to C++ also. The most important facilities that C++ adds on to classes, inheritance, function overloading and operator overloading.The object oriented features in c++ allow programmers to build large programs with clarity , extensibility and eace of maintenance incorporating spirit and efficiency of C.

# Objects # Classes # Data abstraction and encapsulation # Inheritance # Polymorphism

Objects:
An object represents an individual, identifiable item, unit, or entity, either real or abstract, with a well-defined role in the problem domain. Objects are the basic run time entities in an object-oriented system.They may represent a person,a place,a bank account,a table of data or any item that the program has to handle.They may also represent user-defined data such as vectors,time and lists. In C++, the class variables are called objects. With objects we can access the public members of a class using a dot operator. class item { ............ ............. ............. } x,y,z; Here item is an object

Classes:
A class is an extension of the idea of structure used in C. A class can have both variables and functions as members. The only difference between a structure & a class in c++ is that , by default the members of a class are private by default , the members of a structure are public. Generally a class specification has two parts :
Class declaration Class function definitions

A simple class example: class item { int number; float cost; public : void getdata(inta ,float b); void put data(void) };

Data Abstraction and Encapsulation:


The wrapping up of data and functions into a single unit is known as encapsulation. Abstraction refers to the act of representing essential features without including the background details or explanations.

Polymorphism:
Polymorphism is the ability to take more than one form. The process of making an operator to exhibit different behaviors in different instances is known as operator overloading.

Inheritance:
Inheritance is the process by which objects of one class acquire the properties of objects of another class

Since C++ allows us to create hierarchy related objects, we can build special object oriented libraries which can be used later by many programmers. While C++ is able to map the real-world problem property , the C part of C++ gives the language the ability to get close to the machine level details. C++ programs are easily maintainable and expandable. It is expected that C++ will replace C as a general purpose language in the near future.

# include <iostream> # include <conio.h> void main() { cout<<C++ Programming.; getch(); }

The functions declared inside the class is known as member function. Defining member functions :
The general form of member function definition is: return type class-name :: function-name (argument declaration) { function body }

Characteristics of member functions:

# Several different classes can use the same function name . The member label will resolve their scope. # Member functions can access the private data of the class. A non-member function cant do so. # A member function can call another member function directly, without using the dot operator.

Functions in C++

Like C in C++ there is a main function in each program. Function prototyping is one of the major improvements added to C++ functions. Every function can be overloaded. Small functions can be made inline to save time and memory space.

Function prototyping
Function prototyping is a declaration statement in the calling program and is of the following form: type function-name(argument-list); Example : float area(float x,float y);

Inline functions
An inline function is a function that is expanded in line when it is invoked. The general format of the inline functions is inline return_type function_name(argument) { //Function body }

Its purpose is to eliminate the cost of time of calls to small functions.

INLINE FUNCTION #include<iostream.h> #include<conio.h> inline float mul(float x, float y) { return(x*y); } inline double div(double p, double q) { return(x/y); } int main() { float a=12.345; cloat b=9.82; cout<<mul(a,b)<<\n; cout<<div(a,b)<<\n; getch(); }

Overhead of a function and return is eliminated here

121.228 1.25713

As we know that the private members cant be accessed from outside the class.That is a non-member function cant have an access to the private data of a class.But C++ allows using friend function to have access to the private data of both classes. The function that are declared with the keyword friend are known as friend function.
To make an outside function friendly to a class,we have to simply declare this function as a friend of the class as shown below:

Class Item { .. .. Public: .. friend void ABC(void); //declaration };

Friend Function

#include<iostream.h> #include<conio.h> class sample { int a; int b; public: void setvalue() {a=25; b=40;} friend float mean(sample s); }; float mean(sample s) { return float(s.a+s.b)/2.0; } int main() { sample X; //object X X.setvalue(); cout<< "Mean value="<<mean(X); getch(); }

The friend function access the class variables a and b by using the dot operator.

Constructors:
A constructor is a special member function whose task is to initialize the objects of its class. It constructs the values of data members of the class.

The general format of defining & declaring constructor:


class integer { Int m,n; public: integer(void); //constructor declared .............. ............... }; integer :: integer (void) // constructor defined { m=0;n=0; } An object created by the class will be initialized automatically. integer int1; Not only creates the object int1 of type integer but also initializes its data members m and n to zero.

Copy constructor :

A copy constructor is used to declare and initialize an object from another object. Integer I2(I1);

Destructors:
A destructor, as the name implies, is used to destroy the objects that have been created by constructor. The general format of destructor is given bellow:
~integer() { } For example, the destructor for the matrix class may be defined as follows: matrix :: ~matrix() { for (int i=0;i<d1;i++) delete p[i]; delete p; }

Operator overloading:

Operator overloading is one of the important features of C++ language. It is called compile time polymorphism. Operator overloading is done with a special function, called operator function.
The general format of operator function is: return type class-name :: operator op(arg-list) { Operator being overloaded function body //task defined }

Requirements:
# Creating a class that defines the data type that is to be used in the overloading operation. # Declearing the operator function operator op() in the public part of the class.It may be either a member function or a friend function. # Define the operator function to implement the required operators.

We cannt overload :
*class member access operator (.,.*) *scope resulation operator (::) *size operator (sizeof) *conditional operator(?:)

#include<iostream.h>

#include<conio.h> class space { int x; int y; int z; public: Void getdata(inta,intb,int c); void display(); void operator-(); //overload unary minus.(declaration) } void space :: getdata(inta,intb,int c) { x=a; y=b; z=c; } void space :: display() { cout<<"x="<<x<<"\n"; cout<<"y="<<y<<"\n"; cout<<"z="<<z<<"\n\n"; } void space :: operator-() Definitions of operator- function { x=-x; y=-y; z=-z; } int main() { space s; s.getdata(10,-20,30); cout<<"S:"; s.display(); -s; activates operator-() function cout<<"-S"; s.display(); getch(); return 0; }

The mechanism of deriving a new class from an old class is called inheritance. Inheritance provides the concept of reusability. The different types of inheritances are given below: #Single Inheritance A derived class with only one base class is called single inheritance. AB #Multiple Inheritance A class can inherit properties from more than one class which is known as multiple inheritance. ACB #Multilevel Inheritance A class can be derived from another derived class which is known as multilevel Inheritance. ABC #Hierarhcical inheritance: One class may be inherited by more than one class.

The general format of defining derived classes class derived class-name : (visibility mode) base class-name { // members of derived class };

#include<iostream.h> #include<conio.h> class B { int a; public: int b; void get_ab(); int get_a(void); void show_a(void);

Base class

}; class D:private B { Inheritance declared int c; public: void mul(void); void display (void); }; void B::get_ab(void) { cout<<"enter values for a and b" ; cin>>a>>b; }

int B::get_a() { return a; } void B::show_a() { cout<<"a="<<a<<"\n"; } void D::mul() { get_ab(); c=b*get_a(); } void D::display() { show_a(); cout<<"b="<<b<<"\n"; cout<<"c="<<c<<"\n"; } int main() { D d; d.mul(); d.display(); d.mul(); d.display(); getch(); return 0; }

A pointer is a variable that contains an address which is location of another variable in the memory. It is a derived data type that refers to another data variable by storing the variables memory address rather than data. A pointer variable defines where to get the value of a specific data variable instead of defining actual data. The general format of pointer variable data-type *pointer-variable;

C++ uses a unique keyword called this to represent an object that invokes a member function. this is a pointer that points to the object for which this function was called . This unique pointer is automatically passed to a member function when it is called. The pointer this acts as an implicit argument to all the member functions. Consider the following simple example class ABC { int a; ...... ...... }; The private variable a can be used directly inside a member function, like a=123;

A simple program using this pointer

#include<iostram.h> #include<cstring.h> #include<conio.h> class person { char name[20]; float age; public: person(char *s,float a) { strcpy (name,s); age=a; } person& person :: greater(person & x) { If(x.age>=age) return x; else return *this; } void display() { cout<<name:<<name<<\n; cout<<age:<<age<<\n; } }; void main() { person p1(Jhon,37.50), p2(ahmed,29.0), p3(hebber,40.25); person p=p1.greater(p3); cout<<Elder person is:<<\n; p.display(); p=p1.greater(p2); cout<<Elder person is:<<\n; p.display(); getch(); }

C++ streams A stream is a sequence of bytes. It acts either as a source from which the input data can be obtained or as a destination to which the output data can be sent. C++ stream classes The C++ I/O system contains a hierarchy of classes that are used to define various streams to deal with both the console and disc files. This classes are called C++ streams classes

A program typically involves either or both of the following kinds of data communication: # Data transfer between the console unit & the program. # Data transfer between the program & a disk file.

opening a file : 1. 2. Using the constructor function Using the member function open() of the class

Opening file using constructor :


# Create a file stream object to manage the stream using the class ofstream to create output stream & the class ifstream to create the input stream. # Initialize the file object with the desired filename.

For example the following statement opens a file named reasults for output
Ofstream outfile(results); // output only Similarly for input: Ifstream infile(data); // input only

The file stream classes support the following functions to manage : seekg() Moves get pointer to a specified location. seekp() Moves put pointer to a specified location. tellg() Gives the current position of the get pointer. tellp() Gives the current position of the PUT pointer.

#include<iostream.h> #include<fstream.h> Provides support for simultaneous i/o #include<conio.h> void main() { ofstream outf(ITEM); cout<<Enter item name:; char name[30]; cin>>name; Write data on file outf<<name<<\n; cout<<Enter item cost:; float cost; cin>>cost; outf<<cost<<\n; outf.close(); Ifstream inf(ITEM); inf>>name; inf>>cost; read data on file cout<<\n; cout<<Item name:<<name<<\n; cout<<Item cost:<<cost<<\n; inf.close(); getch(); Enter item name : CD-R } Enter item cost : 120 item name : CD-R item cost : 120

Thanks to all

You might also like