p. 1
e-528-529 sector-7 dwarka new delhi-110075 nr ramphal chowk and sector 9 metro station ph 011-47350606 m 7838010301-04 www.eduproz.in educate anytime anywhere greetings for the day about eduproz we at eduproz started our voyage with a dream of making higher education available for everyone since its inception eduproz has been working as a stepping-stone for the students coming from varied backgrounds the best part is the classroom for distance learning or correspondence courses for both management mba and bba and information technology mca and bca streams are free of cost experienced faculty-members a state-of-the-art infrastructure and a congenial environment for learning are the few things that we offer to our students our panel of industrial experts coming from various industrial domains lead students not only to secure good marks in examination but also to get an edge over others in their professional lives our study materials are sufficient to keep students abreast of the present nuances of the industry in addition we give importance to regular tests and sessions to evaluate our students progress students can attend regular classes of distance learning mba bba mca and bca courses at eduproz without paying anything extra our centrally air-conditioned classrooms well-maintained library and well-equipped laboratory facilities provide a comfortable environment for learning honing specific skills is inevitable to get success in an interview keeping this in mind eduproz has a career counselling and career development cell where we help student to prepare for interviews our dedicated placement cell has been helping students to land in their dream jobs on completion of the course eduproz is strategically located in dwarka west delhi walking distance from dwarka sector 9 metro station and 4minutes drive from the national highway students can easily come to our centre from anywhere delhi and neighbouring gurgaon haryana and avail of a quality-oriented education facility at apparently no extra cost why choose edu proz for distance learning?
[close]
p. 2
· · · · · · · · · edu proz provides class room facilities free of cost in eduproz class room teaching is conducted through experienced faculty class rooms are spacious fully air-conditioned ensuring comfortable ambience course free is not wearily expensive placement assistance and student counseling facilities edu proz unlike several other distance learning courses strives to help and motivate pupils to get high grades thus ensuring that they are well placed in life students are groomed and prepared to face interview boards mock tests unit tests and examinations are held to evaluate progress special care is taken in the personality development department have a good day karnataka state open university ksou was established on 1st june 1996 with the assent of h.e governor of karnataka as a full fledged university in the academic year 1996 vide government notification no/edi/uov/dated 12th february 1996 karnataka state open university act 1992 the act was promulgated with the object to incorporate an open university at the state level for the introduction and promotion of open university and distance education systems in the education pattern of the state and the country for the co-ordination and determination of standard of such systems keeping in view the educational needs of our country in general and state in particular the policies and programmes have been geared to cater to the needy karnataka state open university is a ugc recognised university of distance education council dec new delhi regular member of the association of indian universities aiu delhi permanent member of association of commonwealth universities acu london uk asian association of open universities aaou beijing china and also has association with commonwealth of learning col karnataka state open university is situated at the northwestern end of the manasagangotri campus mysore the campus which is about 5 kms from the city centre has a serene atmosphere ideally suited for academic pursuits the university houses at present the administrative office academic block lecture halls a well-equipped library guest house
[close]
p. 3
cottages a moderate canteen girls hostel and a few cottages providing limited accommodation to students coming to mysore for attending the contact programmes or termend examinations bc0037-1.1 introduction introduction this unit has the following objectives · to understand the importance of object oriented programming approach over procedural languages · to learn the basic features supported by oop languages · to learn the basic construct of a c program and learn to compile and execute it bc0037-1.2 evolution of programming methodologies evolution of programming methodologies the programming languages have evolved from machine languages assembly languages to high level languages to the current age of programming tools while machine level language and assembly language was difficult to learn for a layman high level languages like c basic fortran and the like was easy to learn with more english like keywords these languages were also known as procedural languages as each and every statement in the program had to be specified to instruct the computer to do a specific job the procedural languages focused on organizing program statements into procedures or functions larger programs were either broken into functions or modules which had defined purpose and interface to other functions procedural approach for programming had several problems as the size of the softwares grew larger and larger one of the main problems was data being completely forgotten the emphasis was on the action and the data was only used in the entire process data in the program was created by variables and if more than one functions had to access data global variables were used the concept of global variables itself is a problem as it may be accidentally modified by an undesired function this also leads to difficulty in debugging and modifying the program when several functions access a particular data.
[close]
p. 4
the object oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data the procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled for example if the programmer had to define his own version of string and define how this new datatype will be manipulated it would be difficult the object oriented programming provides this flexibility through the concept of class another limitation of the procedural languages is that the program model is not closer to real world objects for example if you want to develop a gaming application of car race what data would you use and what functions you would require is difficult questions to answer in a procedural approach the object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality in the car game example we would create several objects such as player car traffic signal and so on some of the languages that use object oriented programming approach are c java csharp smalltalk etc we will be learning c in this text to understand object oriented programming c is a superset of c several features are similar in c and c self assessment questions 1 list the limitations of procedural languages 2 is an oop language 3 in oop approach programmers can create their own data types true/false 4 in procedural languages the programs are written by dividing the programs into smaller units known as bc0037-1.3 introduction to oop and its basic features introduction to oop and its basic features as discussed earlier one of the basic concept in object oriented programming approach is bundling both data and functions into one unit known as object the functions of a particular object can only access the data in the object providing high level of security for the data the functions in the object are known as member functions or sometimes as methods the key features of oop programming languages are · objects and classes
[close]
p. 5
an object is a program representation of some real-world thing i.e person place or an event objects can have both attributesdata and behaviours functions or methods attributes describe the object with respect to certain parameters and behaviour or functions describe the functionality of the object table 1.1 example of objects polygon object attributes behaviour move bank account behaviour attributes deduct funds position fill color border color erase changecolor account number transferfunds balance depositfunds showbalance according to pressman objects can be any one of the following a external entities b things c occurrences or events d roles e organisational units f places g data structures for example objects can be an menu or button in an graphic user interface program or it may be an employee in an payroll application objects can also represent a data structure such as a stack or a linked list it may be a server or a client in an networking environment objects with the same data structure and behavior are grouped together as class in other words objects are instances of a class classes are templates that provide definition to the objects of similar type objects are like variables created whenever necessary in the program for example employee may be a class and pawan sujay and ganesh are objects of the class employees just as you can create as many variables of a default datatype such as integer you can create as many objects of a class classes and objects support data encapsulation and data hiding which are key terms describing object oriented programming languages data and functions are said to be encapsulated in an single entity as object the data is said to be hidden thus not allowing accidental modification.
[close]
p. 6
· inheritance inheritance is one of the most powerful feature of object oriented programming languages that allows you to derive a class from an existing class and inherit all the characteristics and behaviour of the parent class this feature allows easy modification of existing code and also reuse code the ability to reuse components of a program is an important feature for any programming language · polymorphism and overloading operator overloading feature allows users to define how basic operators work with objects the operator will be adding two numbers when used with integer variables however when used with user defined string class operator may concatenate two strings similarly same functions with same function name can perform different actions depending upon which object calls the function this feature of c where same operators or functions behave differently depending upon what they are operating on is called as polymorphism same thing with different forms operator overloading is a kind of polymorphism oop approach offers several advantages to the programmers such as · · code can be reused in situations requiring customization o program modeling and development closer to real world situations and objects easy to modify code o only required data binded to operations thus hiding data from unrelated functions self assessment questions 1 feature in oop allows to reuse code 2 the concept of bundling data and functions into single unit is termed as 3 object is an of a class 4 give an example of a class and an object 5 is an advantage of oop approach bc0037-1.4 basic components of a c program and program structure 1.4 basic components of a c program and program structure
[close]
p. 7
the c is a superset of c at the basic level the programs look similar in both c and c every statement in c ends with a semicolon all the reserved words have to be written in small case and the c compiler is case sensitive data in programming languages are stored in variables to create a variable the variable should support an inbuilt datatype the variable is a name given to a particular location in the memory and the value stored in a variable can be altered during program execution the datatypes supported in c are listed below table 1.2 basic datatypes in c data type size in bytes int 2 bool 1 char 1 long 4 float 4 double 8 long double 10 unsigned int 2 values that can be taken -32768 to 32767 false and true 0 and 1 -128 to 127 -2,147,483,648 to 2,147,483,647 3.4 x 10-38 to 3.4 x 1038 precision 7 1.7 x 10-308 to 1.7 x 10308 precision 15 3.4 x 10-4932 to 1.1 x 104932 precision 19 0 to 65,535 variables can be named according to following rules · · · · can comprise of 1 to 8 alphabets digits or underscore first character should be an alphabet names are case sensitive reserve words of c cannot be used variables have to be declared before using them in the program the declaration is done in the following way datatype variablename eg int data the above declaration declares a integer variable named data the value stored in int data by default is a junk value values can also be assigned to the variable during declarations or initialized separately using the assignment operator eg int data=0 or int data data=0;
[close]
p. 8
constants are those which do not change during execution of a program the constants in c can be numeric character or string constants examples of each are shown in table 1.3 table 1.3 example of constants constant numeric constant character constant string constant example 23000 constraints can be negative or positive cannot contain blanks or 450.6 floating commas or point any character enclosed within single quotes represented by `a unique ascii number in the memory set of characters enclosed in double quotes last character in hello the string is null character 0 operators supported in c are listed below unary operators are used with one operand and binary operator is used with two operands table 1.4 operators in c arithmetic operators relational operators logical operators action unary as well as binary subtraction for binary and minus for unary binary addition binary multiplication binary division binary modulus remainder after dividing unary decrement value by one unary increment value by one type action binary greater than binary greater than or equal binary less than binary less than equal binary comparision for equality binary comparision for inequality type action binary and binary or unary not type lets begin with a simple c program sum.cpp
[close]
p. 10
comment statements can be included in the program by prefixing the statement with for single line comments comments add clarity to the program multiple line comments can be added by enclosing the statements between and self assessment questions 1 is a header file used in c that handles input and output functions 2 statement is used to input data from the user in c 3 statement is used to display data on the display screen in c 4 is a master function required in all c program and program execution begins from the first statement of this function bc0037-1.5 compiling and executing c program compiling and executing c program 1 there are three steps in executing a c program compiling linking and running the program the c programs have to be typed in a compiler all the programs discussed in the book will be compiled on turbo c compiler the turbo c compiler comes with an editor to type and edit c program after typing the program the file is saved with an extension .cpp this is known as source code the source code has to be converted to an object code which is understandable by the machine this process is known as compiling the program you can compile your program by selecting compile from compile menu or press alt+f9 after compiling a file with the same name as source code file but with extension .obj is created second step is linking the program which creates an executable file .exe filename same as source code after linking the object code and the library files cs.lib required for the program in a simple program linking process may involve one object file and one library file however in a project there may be several smaller programs the object codes of these programs and the library files are linked to create a single executable file third and the last step is running the executable file where the statements in the program will be executed one by one fig 1.1 shows the entire process when you execute the program the compiler displays the output of the program and comes back to the program editor to view the output and wait for user to press any key to return to the editor type getch as the last statement in the program getch is an inbuilt predefined library function which inputs a character from the user through standard input however you should include another header file named conio.h to use this function conio.h contains the necessary declarations for using this function the include statement will be similar to iostream.h.
[close]
p. 11
fig 1.1 compiling and linking during compilation if there are any errors that will be listing by the compiler the errors may be any one of the following 1 syntax error this error occurs due to mistake in writing the syntax of a c statement or wrong use of reserved words improper variable names using variables without declaration etc examples are missing semi colon or paranthesis type integer for int datatype etc appropriate error message and the statement number will be displayed you can see the statement and make correction to the program file save and recompile it 2 logical error this error occurs due to the flaw in the logic this will not be identified by the compiler however it can be traced using the debug tool in the editor first identify the variable which you suspect creating the error and add them to watch list by selecting debug watches add watch write the variable name in the watch expression after adding all the variables required to the watch list go to the statement from where you want to observe if you are not sure you can go to the first statement of the program then select debug toggle breakpoint or press ctrl f8 a red line will appear on the statement then run the program by selecting ctrl f9 or run option from run menu the execution will halt at the statement where you had added the breakpoint the watch variables and their values at that point of time will be displayed in the bottom in the watch window press f8 to execute the next statement till you reach the end of the program in this way you can watch closely the values in the watch variables after execution of each and every statement in the program if you want to exit before execution of the last statement press ctrl break to remove the breakpoint in the program go to the statement where you have added breakpoint select debug toggle breakpoint or press ctrl f8 select debug watch >remove watches to remove the variables in the watch list this tool helps in knowing the values taken by the variable at each and every step you can compare the expected value with the actual value to identify the error 3 linker error this error occur when the files during linking are missing or mispelt 4 runtime error
[close]
p. 12
this error occurs if the programs encounters division by zero accessing a null pointer etc during execution of the program self assessment questions 1 and are phases in execution of a c program 2 the logical error is identified by the compiler true/false 3 is the extension of c program source files 4 is the extension of c object code bc0037-1.6 summary summary object oriented programming enables storing data and functions together which enables hiding data from unnecessary exposure procedural languages differs from the object oriented programming in the approach used in solving the problem while the former focuses on organizing programs around functions the later focuses organizing programs around classes classes allow users to define their own datatypes and functionality this allows extension of the basic datatypes supported by the language reusability of code through inheritance allows users to use the existing code without modifying it but also extend the functionality of the existing code the c programs are similar to c except for the object oriented programming features every c program has a main function from where the execution starts c programs goes through two phases ie compiling and linking before execution bc0037-2.1 introduction introduction this unit has the following objectives · to learn to use selection control statements in c · to learn to use loops or iteration in c · to implement programs using relational logical increment and decrement operators in c
[close]
p. 13
bc0037-2.2 selection control statements in c selection control statements in c there are basically two types of control statements in c which allows the programmer to modify the regular sequential execution of statements.they are selection and iteration statements the selection statements allow to choose a set of statements for execution depending on a condition if statement and switch statement are two statements which allow selection in c there is also an operator known as conditional operator which enables selection if statement syntax if expression or condition statement 1 statement 2 else statement 3 statement 4 the expression or condition is any expression built using relational operators which either yields true or false condition if no relational operators are used for comparison then the expression will be evaluated and zero is taken as false and non zero value is taken as true if the condition is true statement1 and statement2 is executed otherwise statement 3 and statement 4 is executed else part in the if statement is optional if there is no else part then the next statement after the if statement is exceuted if the condition is false if there is only one statement to be executed in the if part or in the else part braces can be omitted following example program implements the if statement evenodd.cpp include
[close]
p. 14
void main int num cout please enter a number endl cin num if num%2 0 cout num is a even number else cout num is a odd number getch the above program accepts a number from the user and divides it by 2 and if the remainder remainder is obtained by modulus operator is zero it displays the number is even otherwise as odd we make use of the relational operator to compare whether remainder is equal to zero or not nested if statement if statement can be nested in another if statement to check multiple conditions if condition1 if condition 2 statement1 statement2 else if condition3 {statement3 }
[close]
p. 15
else statement4 the flowchart of the above example is shown below fig 2.1 nested if statement multiple conditions can be checked using logical operatorand and operator or if condition1 condition2 statement1 else statement2 in the above example statement1 will be executed if both the condition1 and condition2 are true and in all other cases statement2 will be executed if condition1 condition2 statement1 else statement2 in the above example statement1 will be executed if either condition1 or condition2 are true and even if both are true statement2 will be executed if both the conditions are false the following program demonstrates the use of operator and nested if statement.
[close]