Notes on data structures and algorithms, C++, Exams of Data Structures and Algorithms

Notes are for those students who feel problems to understand data structure

Typology: Exams

2016/2017

Uploaded on 07/02/2017

bishwas-pokharel
bishwas-pokharel 🇳🇵

4

(2)

4 documents

1 / 128

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
BEST OF LUCK
Object oriented
programming
A complete note
By Bishwas Pokharel
068/bct/515(pulchowk campus)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Notes on data structures and algorithms, C++ and more Exams Data Structures and Algorithms in PDF only on Docsity!

BEST OF LUCK

Object oriented

programming

A complete note

By Bishwas Pokharel 068/bct/515(pulchowk campus)

1. Introduction to Object Oriented Programming 1.1 Issues with Procedure Oriented Programming Global variables are visible throughout the program and can be accessed by all function of program. So if any function uses the local variable with the same declaration as global variable then value of local variable is used in place of global variable throughout the function. If program length is long and by some dilemma value of global variable is changed then It is difficult to detect exact location where error takes place. #include #include<conio.h> using namespace std; int a=2; void add(){ cout<<"The value of global variable a in add function: "<<a<<endl; a=3; value of global variable changes to 3. cout<<"The value of global variable a in add function changes to : "<<a<<endl; int a=4; cout<<"The value of local variable a: "<<a<<endl; } int main(){ cout<<"The value of global variable a in main function: "<<a<<endl; add(); cout<<"The value of global variable a in main function after function : "<<a<<endl; getche(); return 0; } Output: The value of global variable a in main function: 2 The value of global variable a in add function: 2 The value of global variable a in add function changes to: 3 The value of local variable a: 4 The value of global variable a in main function after function: 3

1.2/1.4 Basic features of Object Oriented Programming (Exam Point of view)

1. Class: Class is an interface to create an object. Class has functions and attributes bind together. Function defines the behavior of an object/s and attribute defines the property of an object/s which it holds .From single class any number of an object can be created. Ex class Car{ int weight; int height; string color; void start(){ cout<<"Start"<<endl; } void run(){ cout<<"Run"<<endl; } } Here, Class Car has attributes as weight, height, color and function as start () and run (). 2. Object: Object is a run time entity. Function and attributes comes into play only after creating an object. This refers that features are defined on class where access of these features are possible only after creating an object. Ex Car lamborghini, ferari; Here Lamborghini and ferari are objects of a class Car. You can use start and run function only after real manufacturing of lamborghini, ferari. Note: For easy visualization, compare class with drawing of a car. By using single drawing many car can be manufactured which represents an object in C++.

3. Data Hiding: Data Hiding is the process of hiding an information (data) from an unauthentic users. Information is provided only after the proper validation. If validation fails then information is not shown. Ex I. In ATM machine, people are able to access account information only after proper validation of pin number used for respective ATM card. If pin number goes wrong at the time of validation then information is secure. II. In Facebook, Gmail and others social sites, Password and ID entered must have to match with the previously stored Password and ID, resides in database to view Information. Advantages: It provides security. 4. Abstraction: Abstraction says the detail mechanism is not necessary, just basic knowledge of using system’s features is sufficient for proper utilization of system. Ex To use ATM machine people does not have to learn how ATM card is validated after entering into machine, which language is used to validate data, which databases either SQL or Oracle is implemented in bank to store data. Just basic knowledge of how to use ATM machine is sufficient to access account information. Here detail mechanism is hidden and only basic feature is provided and it’s called an abstraction. Advantages: It provides simplicity.

7. Polymorphism: Polymorphism refers to many forms. This means under different situation same thing can behave differently. We human being is best example of polymorphism. In university a person is a student, In home same person can be son/daughter of parent or husband/wife or father/mother of their child. According to location, relation changes for same person. In OOP, same concept applies in polymorphism. Function Overloading and Dynamic Binding is an example of polymorphism. Ex #include using namespace std; void m1(int i){ cout<<"int-args:"<<endl; } void m1(float f){ cout<<"float-args:"<<endl; } int main(){ m1(2); m1(2.3f); return 0; } Output: int-args: float-args: Here, both functions have same name but depending upon type of argument it gives different output. If int argument is passes then it shows “int-args” as output. If float argument is passes then it shows “float-args” as output. Advantages: It provides flexibility.

1.3 Procedure oriented versus Object oriented programming

1. Procedural Programming languages (POP) follow T op Down

approach. OOP follows Bottom Up approach.

2. In POP, Importance is not given to data but to functions as well as sequence of

actions to be done.

In OOP, Importance is given to the data rather than procedures or functions because it

works as a real world.

3. In POP, program is divided into small parts called functions.

In OOP, program is divided into parts called objects.

4. To add new data and function in POP is not so easy.

OOP provides an easy way to add new data and function.

5. In POP, Overloading is not possible.

In OOP, loading is possible in the form of Function Overloading and Operator

Overloading.

“Fall seven times and stand up eight” – Japanese Proverb

Procedure Oriented Programming vs Object Oriented Programming

Divided Into In POP, program is divided into small parts In OOP, program is divided into parts called objects. called functions. Importance In POP, Importance is not given to data but to In OOP, Importance is given to the data rather than functions as well as sequence of actions to be procedures or functions because it works as a real done. world. Approach POP follows Top Down approach. OOP follows Bottom Up approach. Access POP does not have any access specifier. OOP has access specifiers named Public, Private, Specifiers Protected, etc. Data Moving In POP, Data can move freely from function to In OOP, objects can move and communicate with each function in the system. other through member functions. Expansion To add new data and function in POP is not so OOP provides an easy way to add new data and easy. function. Data Access In POP, Most function uses Global data for sharing In OOP, data can not move easily from function to that can be accessed freely from function to function,it can be kept public or private so we can function in the system. control the access of data. Data Hiding POP does not have any proper way for hiding data OOP provides Data Hiding so provides more security. so it is less secure. Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the form of Function Overloading and Operator Overloading. Examples Example of POP are : C, VB, FORTRAN, Pascal. Example of OOP are : C++, JAVA, VB.NET, C#.NET.

“Life is not measured by the number of breaths we take, but by the moments

that take our breath away”–Maya Angelou

Bishwas Pokharel(068/BCT/515) pulchowk camp

  1. lntroduction to C+ +

i. If you know C/C++ better, you can easily understand java, javascript, and many other

C-style languages

ii. If you want to find out some implementation detail about operation system, linux or

minix would be the best choice, and they are both written in C/C++.

iii. There're tremendous excellent tools/libraries that are written in C/C++, which you

can use to build the basic blocks of your project.

iv. If you want to write programs(Android apps, server side programs, etc.) the are

secure and efficient, you should consider using C/C++.

2.2 Features of C+ +

Same as 1.2/1.

2.3 C+ + Versus C

➢ C++ is backward compatible with C

➢ C++ supports OOP

➢ C++ standard library contains lot more functionality

Improved I/O mechanism

➢ Lot of new header files

➢ C is very efficient, C++ is close too.

2.1 The Need of C+ +

However, in 1983, the name was changed to C++. C++ extends C by adding object-oriented

features. Because C++ is built upon the foundation of C, it includes all of C's features,

attributes, and benefits. This is a crucial reason for the success of C++ as a language.

The invention of C++ was not an attempt to create a completely new programming

language. Instead, it was an enhancement to an already highly successful one. C++ was

standardized in November 1997, and an ANSI/ISO standard for C++ is now available.

You may be disappointed if you fail, but you are doomed if you don’t try.–Beverly Sills

Bishwas Pokharel(068/BCT/515) pulchowk campus

3. C++ Language Constructs 3.1 General C++ Program Structure #include 1 using namespace std; 2 int main(){ 3 cout<<”hello world”<<endl; 4 return 0; **}

  1. #include** This line is a preprocessing directive. All preprocessing directives within C++ source code begin with a # symbol. This one directs the preprocessor to add some predefined source code to our existing source code before the compiler begins to process it. This process is done automatically and is invisible to us. 2. Using namespace std The two items our program needs to display a message on the screen, cout and endl , have longer names: std::cout and std::endl. This Using namespace std directive allows us to omit the std:: prefix and use their shorter names. name std stands for “standard,” and the Using namespace std line indicates that some of the names we use in our program are part of the so-called “standard namespace.” 3. Int main() This specifies the real beginning of our program. Here we are declaring a function named main. Q. Why always main? Is it possible to use any other name besides main? Ans: Compiler has instruction to search for keyword main. If compiler is instructed to search for other name as your choice besides main then the program must have you search name which you instruct to search for compiler. Return value 0 is also not necessary, you can return any integer value. But compiler does not care about any value you return. So it is considered better to return 0.

3.2.2 Identifiers. An identifier is a sequence of characters used to denote one of the following: Object or variable name Class, structure, or union name Enumerated type name Member of a class, structure, union, or enumeration Function or class-member function typedef name Label name Macro name Macro parameter Rules for writing an identifier A valid identifier can have letters (both uppercase and lowercase letters), digits and underscores. The first letter of an identifier should be either a letter or an underscore. However, it is discouraged to start an identifier name with an underscore. There is no rule on length of an identifier. However, the first 31 characters of identifiers are discriminated by the compiler. Identifier must be unique. They are created to give unique name to a entity to identify it during the execution of the program. For example:

int money;

double accountBalance;

Here, money and accountBalance are identifiers.Also remember, identifier names must

be different from keywords. You cannot use int as an identifier because int is a keyword.

3.2.3 Constants/Literals A constant is a value or an identifier whose value cannot be altered in a program. For example: 1, 2.5, "C programming is easy", etc. As mentioned, an identifier also can be defined as a constant.

Suppose we are writing a program to determine which members of a population are

eligible to vote, permitted to drink, both or neither.

const DRINKING_AGE = 21;

const VOTING_AGE = 18;

18 and 21 are literals. We can use these literals in all areas of our program. For example, if(age > 18) or if(age < 21). But we can make our code more understandable if we use constants instead. if(age > VOTING_AGE) is easier to understand. Other benefits of using constants are Constants free the programmer from having to remember what each literal should be. Often values that stay constant throughout the program have a business meaning. If there are several such values, the programmer can define them all in the beginning of the program and then work with the easier-to-remember constant names. If business requirements dictate that the constant be changed (for example, if the drinking age is lowered to 20 in the future), it is much easier to adapt the program. If we use literals throughout the program, the change will be hard to do and there is a good chance some instances will not be corrected. 3.2.4 Operators and Punctuators

Punctuators: They are mostly used for formatting statements and expressions.

Examples are [ ] { } ( ) , ; : * = etc.

Operators: They are used for computations on data items. They are of following types:

Arithmetic Operators : +, - , *, /

Increment/Decrement Operators : ++,–

Relational Operators: ==, <, >, <=, >=, != Logical operators :

Conditional Operators : ?:

  1. User Defined Data Types : class, structure
  2. Derived Data Types: function, array, pointer Type modifiers: Type modifiers alternate the meaning of data types. There are 4 types of type modifiers. They are unsigned, signed, short (2 byte), long (4byte). Practical example: Suppose you want to make a ticket booking system. Tick amount may be in negative value for which it is better to used signed modifiers and amount may be long number so better to used long, for ticket number it is better to used unsigned modifier and ticket number is small in length so better to used short. Template for variable declaration: Type modifiers (signed/unsigned) or Type modifiers (short/long) or primitive data type + variable name. Ex signed short int; - > convert 4 byte int into 2 byte unsigned long int
  • convert 4 byte int into 4 byte **Rules:

  1. Char :** Char is used with only unsigned and signed as **i. Unsigned char ii. Signed char
  2. Int :** Int is used with all types of modifiers. i. Signed short int (2 byte) ii. Signed long int (4 byte) iii. Unsigned short int (2 byte) iv. Unsigned long int (4 byte)

3. Long: Long is used with all modifiers except short. i. Signed long long (8 byte) **ii. Unsigned long long(8 byte)

  1. Float:** Float does not prefix any modifiers. 5. Double Double used only long among modifiers I. Long double (12 byte) Finding Range: i. Unsigned char: ( 1 byte= 8bit) In unsigned case there is no case of positive or negative sign. It is always positive number where lowest value= 0 0 0 0 0 0 0 0 And highest value = 255 1 1 1 1 1 1 1 1 11111111 binary value to decimal is, =1×pow(2,7)+1×pow(2,6)+1×pow(2,5)+1×pow(2,4)+1×pow(2,3)+1×p ow(2,2)+1×pow(2,1)+1×pow(2,0) =1+2+4+8+16+32+64+128 where a=1,r=2,n=8 in geometric series. =a (rn^ − 1 )/(r-1) = ((2^8)-1) = 255 ii. Signed char ( 1 byte= 8bit) In signed case there is case of positive or negative sign. For positive case MSB is zero whereas for negative case MSB is 1 with negative sign.