0% found this document useful (0 votes)
4 views18 pages

Module 1

The document provides an overview of programming languages, specifically differentiating between Procedure Oriented Programming (POP) and Object Oriented Programming (OOP). It outlines key concepts of OOP such as classes, objects, inheritance, polymorphism, and encapsulation, along with their benefits and applications. Additionally, it compares C and C++ languages, highlighting the features and structure of C++ programming.

Uploaded by

sonyramesh2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views18 pages

Module 1

The document provides an overview of programming languages, specifically differentiating between Procedure Oriented Programming (POP) and Object Oriented Programming (OOP). It outlines key concepts of OOP such as classes, objects, inheritance, polymorphism, and encapsulation, along with their benefits and applications. Additionally, it compares C and C++ languages, highlighting the features and structure of C++ programming.

Uploaded by

sonyramesh2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Module 1:

The high-level programming languages are broadly categorized in to two categories:

1) Procedure oriented programming(POP) language.

2) Object oriented programming(OOP) language.

Procedure Oriented Programming Language

In the procedure oriented approach, the problem is viewed as sequence of things to be done such as
reading , calculation and printing.

Procedure oriented programming basically consist of writing a list of instruction or actions for the
computer to follow and organizing these instruction into groups known as functions.

The disadvantage of the procedure-oriented programming languages is:

1. Global data access

2. It does not model real-world problems very well

3. No data hiding

Characteristics of procedure oriented programming:

1. Emphasis is on doing things(algorithm)

2. Large programs are divided into smaller programs known as functions.

3. Most of the functions share global data

4. Data move openly around the system from function to function

5. Function transforms data from one form to another.

6. Employs top-down approach in program design

Object Oriented Programing

“Object oriented programming as an approach that provides a way of modularizing programs by creating
partitioned memory area for both data and functions that can be used as templates for creating copies of
such modules on demand”.

Features of the Object Oriented programming


1. Emphasis is on doing rather than procedure.

2. programs are divided into what are known as objects.

3. Data structures are designed such that they characterize the objects.

4. Functions that operate on the data of an object are tied together in the data structure.

5. Data is hidden and can’t be accessed by external functions.

6. Objects may communicate with each other through functions.

7. New data and functions can be easily added.

8. Follows bottom-up approach in program design.

BASIC CONCEPTS OF OBJECTS ORIENTED PROGRAMMING

1. Objects

2. Classes

3. Data abstraction and encapsulation

4. Inheritance

5. Polymorphism

6. Dynamic binding

7. Message passing

OBJECTS

Objects are the basic run-time entities in an object-oriented system. They may represent a person, a place,
a bank account or any item that the program must handle.

CLASS:

 A class is a blueprint or template for creating objects. It defines the properties (data members) and
behaviors (member functions) that the objects of the class will have.

Or

In C ++ a class is a new data type that contains member variables and member functions that operate on
the variables.
Eg: class ClassName

{ public:

// Data members (variables)

int data;

// Member functions (methods)

void display()

// Function body }

};

Object

 An object is an instance of a class. It is created using the class definition and can access the
class's members (data and functions).

Syntax:

ClassName obj; // Creating an object of the class

 obj.data = 10; // Accessing data member

obj.display(); // Calling member function

#include <iostream>

using namespace std;

// Define a class

class Car {

public:

string brand; // Data member

int speed;

// Member function
void display() {

cout << "Brand: " << brand << ", Speed: " << speed << " km/h" << endl;

};

int main() {

Car car1; // Create an object of the class

car1.brand = "Toyota"; // Set data members

car1.speed = 120;

car1.display(); // Call member function

return 0;

DATA ABSTRACTION :

Abstraction refers to the act of representing essential features without including the back ground details
or explanations. Classes use the concept of abstraction and are defined as size, width and cost and
functions to operate on the attributes.

DATA ENCAPSALATION :

The wrapping up of data and function into a single unit (called class) is known as encapsulation. The data
is not accessible to the outside world and only those functions which are wrapped in the class can access
it. These functions provide the interface between the objects data and the program.

INHERITENCE :

Inheritance is the process by which objects of one class acquire the properties of another class. In the
concept of inheritance provides the idea of reusablity. This mean that we can add additional features to an
existing class with out modifying it. This is possible by desining a new class will have the combined
features of both the classes

POLYMORPHISIM:
Polymorphism means the ability to take more than one form. An operation may exhibit different instance.
The behaviour depends upon the type of data used in the operation.

A language feature that allows a function or operator to be given more than one definition. The types of
the arguments with which the function or operator is called determines which definition will be used.

Overloading may be operator overloading or function overloading.

It is able to express the operation of addition by a single operater say ‘+’. When this is possible you use
the expression x + y to denote the sum of x and y, for many different types of x and y; integers , float and
complex no. You can even define the + operation for two strings to mean the concatenation of the strings.

DYNAMIC BINDING :

Binding refers to the linking of a procedure call to the code to the executed in response to the call.
Dynamic binding means the code associated with a given procedure call is not known untill the time of
the call at run-time. It is associated with a polymorphic reference depends upon the dynamic type of that
reference.

MESSAGE PASSING :

An object oriented program consists of a set of objects that communicate with each other.

A message for an object is a request for execution of a procedure and therefore will invoke a function
(procedure) in the receiving object that generates the desired result. Message passing involves specifying
the name of the object, the name of the function (message) and information to be sent.

Employee . Salary (name)

Object message information

BENEFITS OF OOP:

Oop offers several benefits to both the program designer and the user. Object-oriented contributes to the
solution of many problems associated with the development and quality of software products. The
principal advantages are :

1. Through inheritance we can eliminate redundant code and extend the use of existing classes.
2. We can build programs from the standard working modules that communicate with one another, rather
than having to start writing the code from scratch. This leads to saving of development time and higher
productivity.

3. This principle of data hiding helps the programmer to build secure programs that can’t be invaded by
code in other parts of the program.

4. It is possible to have multiple instances of an object to co-exist with out any interference.

5. It is easy to partition the work in a project based on objects.

6. Object-oriented systems can be easily upgraded from small to large systems.

7. Message passing techniques for communication between objects makes the interface description with
external systems much simpler.

8. Software complexity can be easily managed.

APPLICATION OF OOP:

The most popular application of oops up to now, has been in the area of user interface design such as
windows. There are hundreds of windowing systems developed using oop techniques.

Real business systems are often much more complex and contain many more objects with complicated
attributes and methods. Oop is useful in this type of applications because it can simplify a complex
problem. The promising areas for application of oop includes.

1. Real – Time systems.

2. Simulation and modeling

3. Object oriented databases.

4. Hypertext,hypermedia and expertext.

5. Al and expert systems.

6. Neural networks and parallel programming.

7. Dicision support and office automation systems.

8. CIM / CAM / CAD system.

Difference between C and C++


Sr.No
Key C C++
.

1 Developer Dennis Richie (1969-1973) Bjarne Stroustrup (1979)

Supports OOPS concepts like


Does not support OOPS
2 OOPS polymorphism, encapsulation, and
concepts.
inheritance.

3 Set C is a subset of C++ C++ is a superset of C

4 keywords C has 32 keywords. C++ has 52 keywords.

Supports
Procedural Programming
5 Paradigm both procedural and object-oriented
Language
programming

Programming
7 Top-Down Approach Bottom-Up Approach
Approach

Data and Functions


Data and Data and Functions
8 are encapsulated together as an
Function are separate
object

Information
9 Not supported Supported via encapsulation.
Hiding

Function and Operator Function and Operator overloading


10 Overloading
overloading are not supported are supported

11 Function Function-driven language. Object-driven language.

C structure does not support C++ Structure supports defining a


12 Structure
defining functions. function.
Memory Dynamic with
13 Manual
Management Constructors/Destructors, new/delete

Reference
14 Not supported Supported
Variables

Virtual and
15 Not supported Supported
friend functions

Exception
16 Not supported Supported
Handling

Basics of C++

C ++ is an object-oriented programming language. C ++ was developed by Jarney Stroustrup at AT & T


Bell lab, USA.

C++ Comments:

C++ introduces a new comment symbol //(double slash). Comments start with a double slash symbol and
terminate at the end of line. A comment may start any where in the line and what ever follows till the end
of line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment. Multi line comments can be written as
follows:

// this is an example of

// c++ program

// thank you

The c comment symbols /* ….*/ are still valid and more suitable for multi line comments.

/* this is an example of c++ program */

Output Operator:

The statement cout <<”Hello, world” displayed the string with in quotes on the screen. The identifier cout
can be used to display individual characters, strings and even numbers. It is a predefined object that
corresponds to the standard output stream. Stream just refers to a flow of data and the standard Output
stream normally flows to the screen display. The cout object, whose properties are defined in iostream.h
represents that stream. The insertion operator << also called the ‘put to’ operator directs the information
on its right to the object on its left.

Return Statement:

In C++ main ( ) returns an integer type value to the operating system. Therefore every main ( ) in C++
should end with a return (0) statement, otherwise a warning or an error might occur.

Input Operator:

The statement

cin>> number 1;

is an input statement and causes. The program to wait for the user to type in a number. The number keyed
in is placed in the variable number1. The identifier cin is a predefined object in C++ that corresponds to
the standard input stream. Here this stream represents the key board.

The operator >> is known as get from operator. It extracts value from the keyboard and assigns it to the
variable on its right

Cascading Of I/O Operator:

cout<<”sum=”<<sum<<”\n”;

cout<<”sum=”<<sum<<”\n”<<”average=”<<average<<”\n”;

cin>>number1>>number2;
Structure Of A Program :

// my first program in C++

#include <iostream>

using namespace std;

int main ()

cout << "Hello World!";

return 0;

Output:-Hello World!.

// my first program in C++

This is a comment line. All lines beginning with two slash signs (//) are considered comments and do not
have any effect on the behavior of the program. The programmer can use them to include short
explanations or observations within the source code itself. In this case, the line is a brief description of
what our program is.

#include <iostream>

Lines beginning with a hash sign (#) are directives for the preprocessor. They are not regular code lines
with expressions but indications for the compiler's preprocessor. In this case the directive
#include<iostream> tells the preprocessor to include the iostream standard file. This specific file
(iostream) includes the declarations of the basic standard input-output library in C++, and it is included
because its functionality is going to be used later in the program.

using namespace std;


All the elements of the standard C++ library are declared within what is called a namespace, the

namespace with the name std.

int main ()

The main function is the point by where all C++ programs start their execution, independently of its
location within the source code. It does not matter whether there are other functions with other names
defined before or after it – the instructions contained within this function's definition will always be the
first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs
have a main function.

cout << "Hello World!";

cout represents the standard output stream in C++, and the meaning of the entire statement is to insert a
sequence of characters (in this case the Hello World sequence of characters) into the standard output
stream (which usually is the screen).

return 0;

The return statement causes the main function to finish. return may be followed by a return code (in our
example is followed by the return code 0). A return code of 0 for the main function is generally
interpreted as the program worked as expected without any errors during its execution. This is the most
usual way to end a C++ console program.

STRUCTURE OF C++ PROGRAM

 Include files

 Class declaration

 Class functions, definition

 Main function program

Example :-

# include<iostream.h>

class person

char name[30];
int age;

public:

void getdata()

cout<<”enter name”;

cin>>name;

cout<<”enter age”;

cin>>age;

void display()

cout<<”\n name:”<<name;

cout<<”\n age:”<<age;

};

int main( )

person p;

p.getdata();

p.display();

return 0;

Additional information
CLASS:-

Class is a group of objects that share common properties and relationships .In C++, a class is a new data
type that contains member variables and member functions that operates on the variables. A class is
defined with the keyword class. It allows the data to be hidden, if necessary from external use. When we
defining a class, we are creating a new abstract data type that can be treated like any other built in data
type.

Generally a class specification has two parts:-

a) Class declaration

b) Class function definition

the class declaration describes the type and scope of its members. The class function definition describes
how the class functions are implemented.

Syntax:-

class class-name

private:

variable declarations;

function declaration ;

public:

variable declarations;

function declaration;

};

The members that have been declared as private can be accessed only

from with in the class. On the other hand , public members can be accessed from outside the class also.
The data hiding is the key feature of oops. The use of keywords private is optional by default, the
members of a class are private.

The variables declared inside the class are known as data members and the functions are known as
members mid the functions. Only the member functions can have access to the private data members and
private functions. However, the public members can be accessed from the outside the class. The binding
of data and functions together into a single class type variable is referred to as encapsulation.

Syntax:-
class item

int member;

float cost;

public:

void getldata (int a ,float b);

void putdata (void);

The class item contains two data members and two function members, the data members are private by
default while both the functions are public by declaration. The function getdata() can be used to assign
values to the member variables member and cost, and putdata() for displaying their values . These
functions provide the only access to the data members from outside the class.

CREATING OBJECTS:

Once a class has been declared we can create variables of that type by using the class name.

Example:

item x;

creates a variables x of type item. In C++, the class variables are known as objects. Therefore x is called
an object of type item.

x, y ,z also possible.

class item

-----------

-----------

-----------

}x ,y ,z;

would create the objects x ,y ,z of type item.

ACCESSING CLASS MEMBER:

The private data of a class can be accessed only through the member functions of that class. The main()
cannot contains statements that the access number and cost directly.
Syntax:

object name.function-name(actual arguments);

Example:- x. getdata(100,75.5);

It assigns value 100 to number, and 75.5 to cost of the object x by implementing the getdata() function .

similarly the statement

x. putdata ( ); //would display the values of data members.

x. number = 100 is illegal .Although x is an object of the type item to which number belongs , the number
can be accessed only through a member function and not by the object directly.

Example:

class xyz

Int x;

Int y;

public:

int z;

};

---------

----------

xyz p;

p. x =0; error . x is private

p, z=10; ok ,z is public

DEFINING MEMBER FUNCTION:

Member can be defined in two places

• Outside the class definition

• Inside the class function

OUTSIDE THE CLASS DEFlNAT1ON;


Member function that are declared inside a class have to be defined separately outside the class.Their
definition are very much like the normal functions.

An important difference between a member function and a normal

function is that a member function incorporates a membership.Identify label in the header. The ‘label’
tells the compiler which class the function belongs to.

Syntax:

return type class-name::function-name(argument declaration )

function-body

The member ship label class-name :: tells the compiler that the function function -name belongs to the
class class-name . That is the scope of the function is restricted to the class-name specified in the header
line. The :: symbol is called scope resolution operator.

Example:

void item :: getdata (int a , float b )

number=a;

cost=b;

void item :: putdata ( void)

cout<<”number=:”<<number<<endl; cout<<”cost=”<<cost<<endl;

The member function have some special characteristics that are often used in the program development.

• Several different classes can use the same function name. The "membership label" will resolve their
scope, member functions can access the private data of the class .A non member function can't do so.

• A member function can call another member function directly, without using the dot operator.
Write a simple program using class in C++ to input subject mark and prints it.

class marks

private :

int ml,m2;

public:

void getdata()

cout<<”enter 1st subject mark:”;

cin>>ml;

cout<<”enter 2nd subject mark:”;

cin>>m2;

void displaydata()

cout<<”Ist subject mark:”<<ml<<endl ;

cout<<”2nd subject mark:”<<m2;

};

Int main()

marks x;
x.getdata();

x.displaydata();

return 0;

You might also like