Liberty collage Fundamentals of C++ programming Languages
Programming Methodologies: Structured and Object-Oriented Programming Two popular approaches to programming design are the structured approach and the object oriented approach. Until recently, programs were thought of as a series of procedures that acted upon data. A procedure or function is a set of specific instructions executed one after the other. The data was quite separate from the procedures, and the trick in programming was to keep track of which functions called which other functions, and what data was changed. To make sense of this potentially confusing situation, structured programming was created .
Continue… The principle ideas behind structured programming are as simple as the idea of divide and conquer . A computer program can be thought of as consisting of a set of tasks. Any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. Structured programming that divides a problem into smaller sub problems is called structured design.
This process of implementing a structured design is called structured programming. The structured-design approach is also known as top-down design, bottom-up design, stepwise refinement, and modular programming As an example, computing the average salary of every employee of a company is a rather complex task. You can, however, break it down into these subtasks: I . Find out what each person earns . II. Count how many people you have . III. Total all the salaries . IV . Divide the total by the number of people you have.
Continue… Totaling the salaries can be broken down into I. Get each employee's record. II . Access the salary . III. Add the salary to the running total. IV . Get the next employee's record. In turn, obtaining each employee's record can be broken down into : I. Open the file of employees . II. Go to the correct record. III . Read the data from dis
Object oriented programming: The essence of object-oriented programming is to treat data and the procedures that act upon the data as a single "object"--a self-contained entity with an identity and certain characteristics of its own . Object-oriented design (OOD) is a widely used programming methodology. In OOD, the first step in the problem-solving process is to identify the components called objects, which form the basis of the solution, and to determine how these objects interact with one another
Continue…. For example, suppose you want to write a program that automates the video rental process for a local video store. The two main objects in this problem are the video and the customer. After identifying the objects, the next step is to specify for each object the relevant data and possible operations to be performed on that data. For example, for a video object, the data might include : Movie name Starring actors Producer Production company Number of copies in stoc
Continue…. Some of the operations on a video object might include : Checking the name of the movie Reducing the number of copies in stock by one after a copy is rented Incrementing the number of copies in stock by one after a customer returns a particular video
There are a few principle concepts that form the foundation of object-oriented programming Object: This is the basic unit of object oriented programming . That is both data and function that operate on data are bundled as a unit called as object . Class: When you define a class, you define a blueprint for an object. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.
Continue…. Abstraction : Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details. For example, a database system hides certain details of how data is stored and created and maintained. Similar way, C++ classes provides different methods to the outside world without giving internal detail about those methods and data
Continue… Encapsulation : Encapsulation is placing the data and the functions that work on that data in the same place. While working with procedural languages, it is not always clear which functions work on which variables but object-oriented programming provides you framework to place the data and the relevant functions together in the same object.
Continue… inheritance : One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class. This is a very important concept of object-oriented programming since this feature helps to reduce the code size
Basic structure of a C++ program Probably the best way to start learning a programming language is with writing a program. So here is our first program: // my first program in C++ output # include << iostream.h > Hello World! int main () { cout << "Hello World!"; return ; }
// my first program in C ++ This is a comment line. All the lines beginning with two slash signs (//) are considered comments and do not have any effect on the behavior of the program. #include Sentences that begin with a pound sign (#) are directives for the preprocessor. They are not executable code lines but indications for the compiler. In this case the sentence #include tells the compiler's preprocessor to include the iostream standard header file. This specific file includes the declarations of the basic standard input-output library in C ++, and it is included because its functionality is used later in the program.
Constants, variables, data types and operators Basic Data types Built in data types 1. integer: They are the numbers without decimal part. Ex: 69, 360, 32330 2. float, double: They are the numbers with decimal point. Ex: 69.65, 3.1415 3. character: Any letter enclosed within single quotes comes under character . Variables Programs need a way to store the data they use. Variables offer various ways to represent and manipulate that data. What is a Variable? A variable is a data name that may be used to store data value. The value of a variable may vary throughout program means that, a variable may take different values at different times during execution
Declaration of variables In order to use a variable in C++, we must first declare it i.e. specifying the data types and data types name we want it to be. The syntax to declare a new variable is to write the data type specifier that we want (like int , short, float...) followed by a valid variable identifier . For example : int a ; int c,d,e ; float mynumber ; double b_c ;
Initialization of variables When declaring a local variable, its value is undetermined by default. But you may want a variable to store a concrete value the moment that it is declared. In order to do that you have to append an assignment sign followed by the value wanted to be assigned to the variable declared: Syntax: type identifier = initial_value ; For example, if we want to declare an int variable called a that contains the value 0 (zero) at the moment in which it is declared , we could write : int a = 0 ; float c = 3.5;
Constants Constant is an expression with a fixed value. Constant expressions consist of only constant values. Constant in C++ refers to fixed values that do not change during the execution of a program. Example: const float pi=3.1415; There also exist non-numerical constants like
Operators An operator is a symbol that tells the computer to perform certain mathematical (or) logical manipulations. Operators used in programs to manipulate data and variables. C++ operators can be classified into number of categories. They include 1 . Arithmetic operators . 2 . Relational operators. 3 . Logical operators . 4 . Assignment operators. 5 . Increment / Decrement operators 6. Conditional operators. 7 . Bitwise operators.
1. Arithmetic operators: : C++ provides all the basic arithmetic operators like add (+), subtract (- ), multiply (*), divide (/), and mod (%). mod gives remainder of division. Ex for mod: if a = 10 ; b = 3; c = a % b; c = 1 ; C ++ code for arithmetic operators output #include void main() { int x=15, y=10, a, s, m, d, e; a= x+y ; s=x-y ; m=x*y ; d=x/y ; e= x%y ; cout <<a<< endl <<s<< endl <<m<< endl <<d<< endl <
2. Relational operators: These are the operators which relate the operands on either side of them like less than (<=), equal to (==), Greater than (>), Greater than or equal (>=) and not equal (! =)
3. Logical operators: C++ has the following three logical operators. && (meaning logical AND), || (logical OR), ! (Logical NOT ).
4. Assignment operators 4. Assignment operators: used to assign the result of an expression to a variable and the symbol used is ‘= ‘it is of 3 types ( i ) Simple assignment a=9; (ii ) Multiple assignment a = b = c = 36 ; (iii) Compound assignment a + = 15; (add 15 to a equal to a =a +15 ;) b - = 5; (subtract 5 from b and assign ). c * = 6; (Multiply c by 6 ). d/=5; ( divide d by 5 equal to d =d /5; ). e % = 10; (divide e by 10 & store
5. Auto increment / decrement (+ + / - -) used to automatically increment and decrement the value of a variable by 1. There are 2 types. 1. Prefix auto increment / decrement --- Adds /subtracts 1 to the operand & result is assigned to the variable on the left . Eg . : a = 5; a=5 ; b=++a; b=--a; Result a=b=6; a=b=4; 2 . Postfix auto increment /decrement --- This first assigns the value to the variable on the left & then increments/decrements the operand . Eg . : a = 5; a=5; b=a++; b=a--; Result b=5, a=6 b=5, a=4
7. Bitwise operators: Performs operation on the bit level. High level language does not support the bitwise operators . 1. Bitwise and (&): This adds corresponding bits in its operands, if both are 1 result is 1 otherwise 0. E.g . if a=8 00001000 b=3 00000011 00000000 =0 (zero) in decimal