Você está na página 1de 29

Training report on c and c++

Prepared By: Sumit Batra 2809203

Overview of C
C is developed by Dennis Ritchie

C is a structured programming language


C supports functions that enables easy maintainability

of code, by breaking large file into smaller modules Comments in C provides easy readability C is a powerful language

Structure Of C Program

Execution Of a C Program
Executing a C program involves following steps:

Creating the program Compiling of Program Linking of Program Execution Of Program

Operators & Expressions


Operator: An operator is a symbol that tells the computer to
perform certain mathematical or logical operations. C supports a set of Operators. There are various Operators in C as Follows: 1. Arithmetic Operators 2. Assignment Operators 3. Increment/Decrement Operators 4. Relational Operators 5. Logical Operators 6. Conditional Operators 7. Bitwise Operators

Functions
A Function is a self-contained program segment that carries out some specific, well-defined task. Each C program consists of one or more functions . One of these functions must be called main. Execution of the program will always begin by carrying out the instructions in main.
Types Of Functions:
C functions can be classified into two categories:

1. Library functions and 2. User-defined functions.


.

Arrays
An Array is a collection of identical data objects which are stored in consecutive memory locations under a common heading or variable name. The objects are called elements of the array and are numbered consecutively 0,1,2,3 and so on. These numbers are called Index Values or Subscripts of the Array. Types of Arrays:

One Dimensional Array Multi Dimensional Array

Pointers
A pointer is a variable that contains a memory address Typically, a pointer contains the address of a variable Addressing operator (&) calculates the address a its operand.
&x /* expression value == address of x */

CS320 - C Programming

Pointers
Declaring a pointer
int *iPtr; float *fPtr;

iptr is a pointer to an integer fptr is a pointer to a float initializing pointers:


int idx = 100; int *ptr = &idx; OR ptr = &idx;
CS320 - C Programming 10

swap the value of two variables

Storage Classes
Determines where the memory is allocated and how long it remains in existance storage classes: o auto o extern o static o register
CS320 - C Programming 12

auto Storage Class


Default storage class for variables declared in the body of a function Memory automatically release on exit from control block Example:
main() { int age; auto int idx;

}
CS320 - C Programming 13

extern Storage Class


Default storage class for variables defined outside a function body. Memory allocated for the life of process Initialized to zero or initial value. Visable to functions that follow definition Example:
int age; main() {...}
CS320 - C Programming 14

static Storage Class


Memory allocated for the life of the process Initialized to zero or initial value. Visable to containing block Maintains value over invocations Example:
myfunc() { static int age;

}
CS320 - C Programming 15

register Storage Class


Compiler recommendation use CPU register otherwise set to auto can only be part of control block
myfunc() { register int age; }
CS320 - C Programming 16

Classifications of Data Types


Built-in data types
Fundamental data types (int, char, double, float, void, pointer) Derived data types (array, string, structure)

Programmer-defined data types


Structure Union Enumeration

Structures
An aggregate, user defined data type used to represent non-simple, abstract data Example:
struct person { char name[50]; char dob[11]; int height; int weight; }; struct person aPerson;
CS320 - C Programming

/* mm/dd/yyyy */ /* in inches */ /* in lbs. */

18

Global and Local Variables


Local Variables
These variables are declared inside some functions. Life time of a local variable is the entire execution period of the function in which it is defined. Cannot be accessed by any other function. In general variables declared inside a block are accessible only in that block.
/* Compute Area and Perimeter of a circle */ #include <stdio.h> float pi = 3.14159; /* Global */ main() { float rad;

/* Local */

printf( Enter the radius ); scanf(%f , &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( Area = %f\n , area ); printf( Peri = %f\n , peri ); } else printf( Negative radius\n); printf( Area = %f\n , area ); }

Global and Local Variables


Global Variables
These variables are declared outside all functions. Life time of a global variable is the entire execution period of the program. Can be accessed by any function defined below the declaration, in a file.
/* Compute Area and Perimeter of a circle */ #include <stdio.h> float pi = 3.14159; /* Global */ main() { float rad;

/* Local */

printf( Enter the radius ); scanf(%f , &rad); if ( rad > 0.0 ) { float area = pi * rad * rad; float peri = 2 * pi * rad; printf( Area = %f\n , area ); printf( Peri = %f\n , peri ); } else printf( Negative radius\n); printf( Area = %f\n , area ); }

An Overview of C++

Introduction
C++ is the C programmers answer to ObjectOriented Programming (OOP). C++ is an enhanced version of the C language. C++ adds support for OOP without sacrificing any of Cs power, or flexibility. C++ was invented in 1979 by Bjarne Stroustrup at Bell Laboratories in Murray Hill, New Jersey, USA.
22

What is OOP?
OOP is a powerful way to approach the task of programming. OOP encourages developers to decompose a problem into its constituent parts. Each component becomes a self-contained object that contains its own instructions and data that relate to that object. So, complexity is reduced and the programmer can manage larger programs.
23

Sample of C++ program #include <iostream.h> int main() { /* program code */ return 0; }

24

Classes: A First Look


General syntax class class-name { // private functions and variables public: // public functions and variables }object-list (optional);
25

Constructors
Every object we create will require some sort of initialization. A classs constructor is automatically called by the compiler each time an object of that class is created. A constructor function has the same name as the class and has no return type. There is no explicit way to call the constructor.
26

Destructors
The complement of a constructor is the destructor. This function is automatically called by the compiler when an object is destroyed. The name of a destructor is the name of its class, preceded by a ~. There is explicit way to call the destructor but highly discouraged.
27

C++ Program
#include<iostream.h> #include<conio.h> class programming { private: int variable; public: void input_value() { cout << "In function input_value, Enter an integer\n"; cin >> variable; } void output_value() { cout << "Variable entered is "; cout << variable << "\n"; } }; main() { programming object; object.input_value(); object.output_value(); //object.variable; Will produce an error because variable is private return 0; }

Thank You

Você também pode gostar