728x90 AdSpace

Monday, 23 December 2019

C++

Introduction

The C++ programming language was developed at AT&T Bell Laboratories in earlier 1980s by Bjarne Stroustrup. He found 'C' lacking for simulations and decided to extend the language by adding features from his favourite language, Simula 67. It was one of the earliest OOPs languages. The name C++ was coined by Rick Mascitti where "++" is the increment operator.

The latest version of  C++ is C++11 or formally C++0x.

The major reason behind the success is that it supports object-oriented technology, the latest software was most near to the real world.

Fundamental Data Type

  1. These are the data types that are not composed of any other data type.
  2. There are 5 Fundamental Data Types
    1. Char
    2. Int
    3. Float
    4. Double
    5. Void
  3. Ex: int a=10;

Derived Data Type

  1. These are the data types that are composed of fundamental data types.
  2. These are
    1. Array
    2. Function
    3. Pointer
    4. Class
    5. Structure
    6. Constant etc.
  3. Ex: int a[10];

Purpose Of Header Files

Header files provide function prototype, the definitions of the library function, declaration of data type and constants with the library functions.

Main Integer

  1. Short
  2. Int
  3. Long
Each comes in both signed and unsigned versions.

Constant

The keyword const can be added to the declaration of an object to make the object a constant rather than a variable.
Constant can't be altered during the program run.

The General Form:

const type name = value;
const int a = 5;

Reference

A reference variable provides an alias for a previously defined variable.

The General Form:

type &reference-variable = variable-name;

Example 

int a = 5;
int &b = a;
cout << a << "\n" << b;

Output 
5

Variable 

It represents the named storage location during the program run.

The General Form:

type name;

int a = 5;

Union 

The union is used to combine two or more different variables.

Error

ERROR CORRECTED
 cout<<"="a; cout<<"="<<a; or cout<<"=a";
m=5, n=12; o=15 m=5, n=12, o=15;
cout<<"x";<<x; cout<<"x"<<x;
cin>>y;>>j; cin>>y>>j;
cin>>"\n">>y; cin>>y;
cout>>"\n"abc"; cout<<"\n abc";
a=b+c a=b+c;
break=x*y; break1=x*y; or a =x*y;

Different Way to initialize

  1. By Separate Assignment Statement. Ex: int age; age=3;
  2. At the time of Declaration. Ex: int age=3;
  3. Dynamic Initialization. Ex: float avg=sum/count;

How Numbers Used in Char?

char a = 75;
It treats 75 as ASCII value and converts it into relative character.

What if const variable is incremented?

const int a=10;
a++;
cout<<a;

Output

10

Arithmetic Operator

  1. Addition (+)
  2. Subtraction (-)
  3. Multiplication (*)
  4. Division (/)
  5. Remainder (%)

Unary Arithmetic Operator

It has only one operand
They are:
  1. ++
  2. --
  3. &
  4. *
  5. +
  6. - etc.
Ex:
short x = 987;
x = -x;

Binary Arithmetic Operator

It has only two operand
They are:
  1. >>
  2. ==
  3. <<
  4. +
  5. - etc.
Ex:
int x = 7, y = 7;
float z;
z = x + y;

Increment Operator

Decrement Operator

++, adds 1 to its operand --, subtract 1 from its operand
Prefix: It performs the increment operation before using the value of the operant. Prefix: It performs the decrement operation before using the value of the operant.
Postfix: First use the value before incrementing the operant value. Postfix: First use the value before decrementing the operant value.

Reasons for Following Invalid expression


Invalid Expression

Reason

asm = 1 || val < 21 In this expression, Keywords can't be used and Single "=" operator is used for comparison which is invalid.
age > 7 && <90 In this expression, Variable name is not mentioned after the "&&" operator.  which is invalid.
a > 50 ||&& v < 50 In this expression, both || and && operators are written together which is invalid.
res !> 20 || !x > 20 In this expression, !> is invalid and !x should written in parenthesis (!x > 20`)


Casting

It is the process of converting data from one type to another.
  1. Type Casting Ex: int a=1, b=2; float c = (float)a/b;
  2. Automatic Type Casting Ex: int a=2; char c = a;



  • Blogger Comments
  • Facebook Comments

0 comments:

Post a Comment

Item Reviewed: C++ Rating: 5 Reviewed By: Unknown