C++ BASICS

CHAPTER 2 (C++ BASICS)

IMPORTANT QUETIONS FOR EXAM (GTU,CVM,SP)


CHAPTER TWO COMPLETE

HERE IS YOUR CVMU SYLLABUS -Link

 BLOG BY JAY SHAH(GMAIL)

 What is C++? Explain the Structure of the C++ Program

  •  C++ is an object-oriented programming language.
  •  It is a superset of C language and also called an extended version of C language.
  •  It was developed by Bjarne Stroustrup at AT&T Bell lab in New Jersey, the USA in the early 1980s.
  •  Structure of the C++ program is as follows.


Include Files

Class Declaration or Definition

Member functions definitions

Main function


  •  In any program first write header files like iostream. h, conio.h, etc..as per the requirement of the program.
  •  After header file write class declaration or definition as per your planning.
  •  After class, define all member functions which are not defined but declared inside the class.
  •  In the last write main function without main function program execution is not possible.

Example:

#include <iostream> //Header File

using namespace std;

class trial //Class

{

int a;

public:

void getdata()

{

a=10;

}

void putdata();

};

void trial::putdata() //Member Function outside class definition

{

cout<<"The value of a = "<<a;

}

int main() //Main Function

{

trial T;

T.getdata();

T.putdata();

}

Output:

The value of a = 10

Namespace:

  •  It defines a scope for the identifiers that are used in a program.
  •  For using identifiers defined in namespace using directive is used as follows:
  • using namespace std;
  •  std is the namespace where ANSI C++ standard class libraries are defined.
  •  All ANSI C++ programs must include this directive.
  •  This will bring all the identifiers defined in std to the current global scope.


Identifiers:

 They refer to the names of variables, functions, arrays, classes, etc., created by the programmer.

 Each language has its own rules for naming these identifiers.

 Following are common rules for both C and C++:

o Only alphabetic characters, digits, and underscores are permitted.

o The name cannot start with a digit.

o Uppercase and lowercase letters are distinct.

o A declared keyword cannot be used as a variable name.


Constants:

 Like variables, constants are data storage locations. But variables can vary, and constants do not change.

 You must initialize a constant when you create it, and you can not assign a new value later after the constant

is initialized.

Defining constant using #define:

 #define is a preprocessor directive that declares a symbolic constant.

Example syntax:

#define PI 3.14

 Every time the preprocessor sees the word PI, it puts 3.14 in the text.

Example:

#include<iostream>

using namespace std;

#define PI 3.14

int main()

{

int r,area;

cout<<”Enter Radius :”;

cin>>r;

area=PI*r*r;

cout<<”Area of Circle = ”<<area;

return 0;

}

Output:

Enter Radius:5

Area of Circle = 78.5

Defining constant using the const keyword:

 ‘const’ keyword is used to declare a constant variable of any type.

 We cannot change its value during the execution of the program.

 Syntax: const DataType Variable_Name=value;

Ex: const int a=2;

 Now ‘a’ is a integer type constant.

Enum: 

Enum is a user-defined type consisting of a set of named constants called enumerator.

 In other words enum is also used to assign numeric constants to strings

 Syntax of enumeration: enum enum_tag {list of variables};

 Example of enumeration: enum day-of-week {mon=1,tue,wed,thu,fri,sat,sun};


Describe various operators used in C++.

An operator is a symbol that tells the compiler to perform certain mathematical or logical operations.

1. Arithmetic Operators

Arithmetic operators are used for mathematical calculation. C++ supports the following arithmetic operators

+ Addition or unary plus

- Subtraction or unary minus

* Multiplication

/ Division

% Modulo division

2. Relational Operators

Relational operators are used to comparing two numbers and taking decisions based on their relation.

Relational expressions are used in decision statements such as if, for, while, etc...

< less than

<= less than or equal to

> greater than

>= greater than or equal to

== is equal to

!= is not equal to


3. Logical Operators

Logical operators are used to testing more than one condition and make decisions

&& logical AND (Both non zero then true, either is zero then

false)

|| logical OR (Both zero then false, either is non zero then true)

! logical NOT (non zero then false, zero then true)


4. Assignment Operators

Assignment operators are used to assigning the result of an expression to a variable. C++ also supports

shorthand assignment operators which simplify operation with assignment

= Assigns the value of right side to left side

+= a += 1 is same as a = a + 1

-= a -= 1 is same as a = a - 1

*= a *= 1 is same as a = a * 1

/= a /= 1 is same as a = a / 1

%= a %= 1 is same as a = a % 1


5. Increment and Decrement Operators

These are special operators in C++ which are generally not found in other languages.

++ Increments value by 1.

a++ is postfix, the expression is evaluated first and then the value is incremented.

Ex. a=10; b=a++; after this statement, a= 11, b = 10.

++a is prefix, the value is incremented first and then the expression is evaluated.

Ex. a=10; b=++a; after this statement, a= 11, b = 11.

-- Decrements value by 1.

a-- is postfix, the expression is evaluated first and then the value is decremented.

Ex. a=10; b=a--; after this statement, a= 9, b = 10.

--a is a prefix, the value is decremented first and then the expression is evaluated.

Ex. a=10; b=--a; after this statement, a= 9, b = 9.

6. Conditional Operator

A ternary operator is known as Conditional Operator.

exp1?exp2:exp3 if exp1 is true then execute exp2 otherwise exp3

Ex: x = (a>b)?a:b; which is same as

if(a>b)

x=a;

else

x=b;

7. Bitwise Operators

Bitwise operators are used to performing operations bit by bit. Bitwise operators may not be applied to float

or double.

& bitwise AND

| bitwise OR

^ bitwise exclusive OR

<< shift left ( shift left means to multiply by 2)

>> shift right ( shift right means divide by 2)


8. Special Operators

& Address operator is used to determining the address of the variable.

* Pointer operator, it is used to declaring pointer variable and to get value from it.

, Comma operator. It is used to link the related expressions together.

size of It returns the number of bytes the operand occupies.

. member selection operator, used in the structure.

-> member selection operator, used in a pointer to structure.


9. Extraction operator (>>)

Extraction operator (>>) is used with cin to input data from the keyboard.

10. Insertion operator (<<)

Insertion operator (<<) is used with cout to output data from the keyboard.

11. Scope resolution operator (::)

Scope resolution operator (::) is used to define the already declared member functions of the class.


Explain the use of the scope resolution operator (::) by giving example.

 The scope resolution operator is used to resolve or extend the scope of the variable.

 C++ is a blocked structured language. We know that the same variable name can be used to have different

meanings in different blocks.

 The scope resolution operator will refer to the value of a global variable from anywhere (also from the inner block)

 Without a scope resolution operator, all variables will refer to a local value.

Example:

#include <iostream.h>

int m=10;

int main()

{

int m=20;

{

int k=m;

int m=30;

cout<<"we are in inner block\n";

cout<<"k="<<k<<"\n";

cout<<"m="<<m<<"\n";

cout<<"::m="<<::m<<"\n";

}

cout<<"we are in outer block\n";

cout<<"m="<<m<<"\n";

cout<<"::m="<<::m<<"\n";

return 0;

}

Output:

we are in the inner block

k=20

m=30

we are in the outer block

m=20

::m=20

TYPECASTING(TYPE CONVERSION)


Implicit Conversion:

 Whenever data types are mixed in an expression, C++ performs the conversions automatically. This process

is known as implicit or automatic conversion.

Example: m= 5+2.5;

 For a binary operator, if the type of the operand differs, the compiler converts one of them to match the

other.

 Using the rule that the smaller type is converted to a wider type.

 For example, if one operand is an integer and another is a float then the integer is converted to float because the float is

wider than the integer.

 In the above example answer means m, will be in float.

Explicit conversion:

 C++ permits explicit type conversion of variables or expressions using the type cast operator.

 Syntax: type_name (expression)

 Example: average = sum/float(i);

 Alternatively, we can use typedef to create an identifier of the required type and use it in the functional

notation.

Example:

typedef int * int_ptr;

p = int_ptr(q);

Example:

#include<iostream>

using namespace std;

int main()

{

int intvar=5;

float floatvar=3.5;

cout<<"intvar = "<<intvar;

cout<<"\nfloatvar = "<<floatvar;

cout<<"\nfloat(intvar) = "<<float(intvar);

cout<<"\nint(floatvar) = "<<int(floatvar);

}

Output:

intvar = 5

floatvar = 3.5

float(intvar) = 5

int(floatvar) = 3


Comments

Popular posts from this blog

CONCEPT OF OOP

Objects and Classes