OOP_C++

Bjarne Stroustrup, of AT&T, developed C++ Programming Language in order to add object oriented constructs to the C language.
  • C++ can be viewed as a procedural language with some additional constructs, some of which are added for object oriented programming and some for improved procedural syntax.
  • A well written C++ program will reflect elements of both object oriented programming style and classic procedural programming.
  • C++ is actually an extensible language. We can define new types in such a way that they act just like the predefined types which are part of the standard language.
  • C++ is designed for large scale software development.
The goal of C++ programming language was to add features that support data abstraction and object oriented concepts to C language.


Introduction

In the most general sense, a program can be organized in one of two ways: around its code (what is happening) or around its data (who is being affected). Using only structured programming techniques, programs are typically organized around code. This approach can be thought of as "code acting on data." For example, a program written in a structured language such as C is defined by its functions, any of which may operate on any type of data used by the program. Object-oriented programs work the other way around. They are organized around data, with the key principle being "data controlling access to code." In an object-oriented language, you define the data and the routines that are permitted to act on that data. Thus, a data type defines precisely what sort of operations can be applied to that data. To support the principles of object-oriented programming, all OOP languages have three traits in common: encapsulation, polymorphism, and inheritance.

Abstraction

Abstraction is hiding complexity of system from the user. Concept of abstraction relates to the idea of hiding data that are not essential.
  • Data abstraction is a programming (and design) technique that relies on the separation of interface and implementation.
  • Let's take one real life example of a TV, which you can turn on and off, change the channel, adjust the volume, and add external components such as speakers, VCRs, and DVD players, BUT you do not know its internal details, that is, you do not know how it receives signals over the air or through a cable, how it translates them, and finally displays them on the screen.
  • Thus, we can say a television clearly separates its internal implementation from its external interface and you can play with its interfaces like the power button, channel changer, and volume control without having zero knowledge of its internals.
  • Now, if we talk in terms of C++ Programming, C++ classes provides great level of data abstraction. They provide sufficient public methods to the outside world to play with the functionality of the object and to manipulate object data, i.e., state without actually knowing how class has been implemented internally.
  • For example, your program can make a call to the sort() function without knowing what algorithm the function actually uses to sort the given values. In fact, the underlying implementation of the sorting functionality could change between releases of the library, and as long as the interface stays the same, your function call will still work.

Encapsulation

Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. In an object-oriented language, code and data may be combined in such a way that they represent a self-contained “black box".
  • When code and data are linked together in this fashion, an object is created. In other words, an Object is the device that supports encapsulation.
  • Within an Object, code, data, or both may be private to that Object. Private code or data is known to and accessible only by another part of the Object. That is private code or data may not be accessed by a piece of the program that exists outside the Object.
  • When code or data is public, other parts of your program may access it even though it is defined within an Object. Typically, the public parts of an Object are used to provide a controlled interface to the private elements of the object.
  • For all intents and purposes, an Object is a variable of a user-defined type. Each time you define a new type of Object, you are creating a new data type. Each specific instance of this data type is a compound variable.

Polymorphism

Object-oriented programming languages support polymorphism, which is characterized by the phrase "one interface, multiple methods." In simple terms, polymorphism is the attribute that allows one interface to control access to a general class of actions. The specific action selected is determined by the exact nature of the situation.
  • A real-world example of polymorphism is a thermostat. No matter what type of furnace your house has (gas, oil, electric, etc.), the thermostat works the same way. In this case, the thermostat (which is the interface) is the same no matter what type of furnace (method) you have. For example, if you want a 70-degree temperature, you set the thermostat to 70 degrees. It doesn't matter what type of furnace actually provides the heat.
  • This same principle can also apply to programming. For example, you might have a program that defines three different types of stacks. One stack is used for integer values, one for character values, and one for floating-point values. Because of polymorphism, you can define one set of names, push() and pop() , that can be used for all three stacks.
  • In your program you will create three specific versions of these functions, one for each type of stack, but names of the functions will be the same. The compiler will automatically select the right function based upon the data being stored. Thus, the interface to a stack— the functions push() and pop() —are the same no matter which type of stack is being used. The individual versions of these functions define the specific implementations (methods) for each type of data.
  • Polymorphism helps reduce complexity by allowing the same interface to be used to access a general class of actions. It is the compiler's job to select the specific action (i.e., method) as it applies to each situation. You, the programmer, don't need to do this selection manually. You need to only remember and utilize the general interface.
  • The first object-oriented programming languages were interpreters, so polymorphism was, of course, supported at run time. However, C++ is a compiled language. Therefore, in C++, both run-time and compile-time polymorphism are supported.

Inheritance

Inheritance is the process by which one object can acquire the properties of another object.
  • This is important because it supports the concept of classification. If you think about it, most knowledge is made manageable by hierarchical classifications. For example, a red delicious apple is part of the classification apple, which in turn is part of the fruit class, which is under the larger class food.
  • Without the use of classifications, each object would have to define explicitly all of its characteristics. However, through the use of classifications, an object need only define those qualities that make it unique within its class. It is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case. As you will see, inheritance is an important aspect of object-oriented programming.


Let's start with a short sample C++ program :

#include<iostream>
using namespase std;
int main()
{
int n;
cout<<"Enter any number:";
// single line comment.
cin>>n; //take input.
//output
cout<<n<<"cube is"<<n*n*n<<endl;
return 0;
}

Output:

Enter any number:5
5cube is 125

 



In C++ programs the header <iostream> is included. This header supports C++ style I/O operation ( <iostream> is to C++ what <stdio.h> is to C). Notice one other thing: there is no .h extension to the name iostream. The reason is the that <iostream> is one of the new style header defined by Standard C++. New-style headers do not use the .h extension.

Namespaces 
The next line in the program is 
using namespace std;

This tells the compiler to use the std namespace. Namespace are a recent addition to C++. A namespace creates a declarative region in which various program elements can be placed. Namespaces help in the organization of large programs. The using statement informs the compiler that you want to use the std namespaces. This is the namespace in which the entire Standard C++ library is declared. By using the std namespace you simplify access to the standard library. The C programs don't need a namespace statement because the C library functions are also available in the default global namespace.




The main function is called at program startup after initialization of the non-local objects with static storage duration. It is the designated entry point to a program that is executed in a hosted environment (that is, with an operating system). The entry points to freestanding programs (boot loaders, OS kernels, etc) are implementation-defined.


Now examine the following line.

int main()

Notice that the parameter list in main() is empty. In C++, this indicates that main() has no parameters. This differs from C. In C, a function that has no parameters must use void in its parameter list. 

The main function has several special properties:

• The main() function doesn't really have to do anything other than be present inside your C++ source code.
• It cannot be called recursively.
• Its address cannot be taken. 
• It cannot be predefined and cannot be overloaded: effectively, the name main in the global namespace is reserved for functions (although it can be used to name classes, namespaces, enumerations, and any entity in a non-global namespace, except that a function called "main" cannot be declared with C language linkage in any namespace (since C++17)).
• It cannot be defined as deleted or declared with C language linkage (since C++17), inline , static, or constexpr.


Comments are an important part of any programming language. They help the person writing a program, and anyone else who must read the source file and understand what's going on. The compiler ignores all comments. Comments do not add to the file size or execution time of the executable program. 


C++ supports single-line and multi-line comments. 

single-line comment example : 

The // (two slashes) characters, followed by any sequence of characters. A new line not immediately preceded by a backslash terminates this form of comment. Therefore, it is commonly called a "single-line comment." 
 
// this is single line comment. 

 multi-line comment example : 

The /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), followed by the */ characters. This syntax is the same as ANSI C. 

 /* C++ comments can also span multiple lines */


There are four built-in data types in C++ : 

• Integer data type 

• Floating point data type 

• Void data type 

• Char data type 

Integer Data Type 

An integer is an integral whole number without a decimal point. These numbers are used for counting. For example 26, 272, -342 are valid integers. Normally an integer can hold numbers from -32768 to 32767. 

Floating point Data Type

 A floating point number has a decimal point. Even if it has an integral value, it must include a decimal point at the end. Valid floating point examples are 45.0, 34.23, -234.34. 

Void Data Type 

It is used for following purposes :

 • It specifies the return type of a function when the function is not returning any value.

• It indicates an empty parameter list on a function when no arguments are passed. 

• A void pointer can be assigned a pointer value of any basic data type. 

Char data type 

It is used to store character value in the identifier (variable/operand).


C++ also permits four types of derived data types. As the name suggests, derived data types are basically derived from built-in data types.

 There are four derived derived data types. 

• Array 
• Function 
• Pointers 
• Reference 

We will study these data types in further chapters.


C++ has four types of user defined data types.

 As the name suggests, user defined data types are defined by the programmers(you) during the development of a software. 

These four user defined data types are : 

• Structure 
• Union 
• Class 
• Enumerator 

We will study these data types in further chapters.


In C++ object oriented language, smaller memory data type variable can be converted to larger data type by the compiler. 

Type conversion can be done by two ways: 

Automatic Type Conversion 

When an expression consists of more than one type of data elements, the C++ compiler converts the smaller data type elements into larger data type element. This process is known as implicit or automatic conversion. 

Typecasting 

Below statement allows the programmer to convert one data type into another data type. 

Syntax 

 aCharVar = static_cast<char>(an IntVar) 

Here in the above syntax char variable will be converted into int variable after execution. 

The C++ language has four typecast operators: 

• static_cast 
• reinterpret_cast 
• const_cast 
• dynamic_cast



A identifier which does not change its value during execution of a program is known as a constant or a literal. 

Any attempt to change the value of a constant will result in an error message. 

A keyword const is added to the declaration of an identifier to make that identifier constant. 

Syntax : 

 const datatype variable = value; 

Example : 

 const float PI = 3.1415
 const char ch = 'A'
 const int rate = 40;

This is the second item's accordion body.

This is the third item's accordion body.

No comments:

Post a Comment