Docsity

Basic of Computer Programming C++, Essays (university) of Computer Science

Partial preview of the text

Download Basic of Computer Programming C++ and more Essays (university) Computer Science in PDF only on Docsity!

Dr. Adarsh Anand

Department of Operational Research

University of Delhi

 C++ is an object oriented programming

language.

 C++ is an extension of C with a major addition

of the class construct feature.

 C++ is superset of C.

 Features like classes, inheritance, function

overloading, and operator overloading make

C++ a truly object-oriented language.

 OO features in C++ allow programmers to build

large programs with clarity, extensibility and

ease of maintenance, incorporating the spirit and

efficiency of C.

Since the invention of computer, many programming approaches

have occurred: Procedural Programming, Modular Programming,

Structural Programming and of-late the OOPS.

A program in a procedural language is a list of instructions where

each statement tells the computer to do something. The focus is on

the proessing, the algorithm needed to perform the desired

computation.

But this proved to be problematic as the data takes a back seat with

this paradigm. Furthermore, this approach does not model the real

world very well.

After this approach came OOPS. This approach views a problem in

terms of objects involved rather than procedure for doing it.

/* Simple hello world program*/

include // This is include directive

int main() { cout << “Hello World!”; //C++ statement return 0 ; } Note: Technically, in C or C++ main function has to return a value because it is declared as "int main“ which means "main function should return integer data type“ if main is declared like "void main", then there's no need of return

  1. Some compilers even accept and compile the code even if u don’t write return 0. varies from compiler to compiler

 Keywords

 Identifiers

 Constants

 Strings

 Operators

 Keywords

 Identifiers

 Constants

 Strings

 Operators

 Keywords

 Identifiers

 Constants

 Strings

 Operators

 Identifiers refers to the names of variables,

functions, arrays, classes , etc. created by the

programmer.

 Rules for creating it.

◦ Only alphabet characters, digits and underscore are permitted. ◦ The name cannot start with digit. ◦ Uppercase and lowercase letters are distinct. ◦ A declared keyword cannot be used as a variable.

Type Bytes Range

signed: - 2147483648 to

  • char 1 - 128 to
    • signed: - 128 to
    • unsigned: 0 to
  • short int 2 - 31768 to
    • signed: - 32768 to
    • unsigned: 0 to
  • int 2 - 32768 to
    • signed: - 31768 to
    • unsigned: 0 to
  • long int 4 - 2147483648 to
    • unsigned: 0 to
  • float 4 3.4E-38 to 3.4E+
  • double 8 1.7E-308 to 1.7E+
  • long double 10 3.4E-4932 to 1.1E+

 Structures and Classes

◦ Basis for OOP. ◦ Classes enable to combine data and procedures.

 Enumerated Data Type

◦ It provides a way to attaching names to the numbers ◦ Increase comprehensibility of the code. ◦ Alternative mean for creating symbolic constants ◦ Enumerates a list of words by assigning them values 0 , 1 , 2 and so on. enum shape {circle, square, triangle}; enum colour {red, blue= 4 , green= 8 };

Functions Set of statements to perform specific tasks. Pointers

  • Special variables to store the memory location of other variables.
  • Used in referencing memory.
  • Concept of constant pointer introduced in c++.
  • Defining a pointer, we use an asterisk between the data type and the variable name:
  • int *ptr; //a pointer to an integer value

Since pointers only hold addresses, when we assign a value to a pointer, the value has to be an address. To get the address of a variable, we can use the address-of operator (&): int nValue = 5; int *pnPtr = &nValue; // assign address of nValue to pnPtr Conceptually, you can think of the above snippet like this: int nValue = 5; int *pnPtr = &nValue; // assign address of nValue to pnPtr cout << &nValue << endl; // print the address of variable nValue cout << pnPtr << endl; // print the address that pnPtr is holding On the author’s machine, this is printed: 0012FF7C 0012FF7C

 Refer to fixed values that do not change in

the execution of the program.

◦ 123 //decimal integer ◦ 12.34 //floating point integer ◦ 037 //octal integer ◦ 0x2 //Hexa decimal ◦ “C++” //string constant ◦ ‘A’ //character constant ◦ L’ab’ //wide-character constant

 Using the qualifier const

◦ const float pi = 3. 14 ;

 Using enum

◦ enum{x,y,z}; ◦ enum{x= 200 ,y= 300 ,z= 400 }; ◦ enum{off,on};