Friday, 24 March 2023

NCERT Class 8th Textbook Mathematics/Ganit/Riyazi

 

Mathematics
Prelims(Open)
Guide for using QR Code(Open)
Chapter 1(Open)
Chapter 2(Open)
Chapter 3(Open)
Chapter 4(Open)
Chapter 5(Open)
Chapter 6(Open)
Chapter 7(Open)
Chapter 8(Open)
Chapter 9(Open)
Chapter 10(Open)
Chapter 11(Open)
Chapter 12(Open)
Chapter 13(Open)
Chapter 14(Open)
Chapter 15(Open)
Chapter 16(Open)
Answers(Open)
Download complete book

Ganit
Prelims(Open)
Guide for using QR Code(Open)
Chapter 1(Open)
Chapter 2(Open)
Chapter 3(Open)
Chapter 4(Open)
Chapter 5(Open)
Chapter 6(Open)
Chapter 7(Open)
Chapter 8(Open)
Chapter 9(Open)
Chapter 10(Open)
Chapter 11(Open)
Chapter 12(Open)
Chapter 13(Open)
Chapter 14(Open)
Chapter 15(Open)
Chapter 16(Open)
Answers(Open)
Download complete book
#ncert #ncertbooks #cbse #class8 #cbsebooks #ncertbooks
Riyazi
Prelims(Open)
Guide for using QR Code(Open)
Chapter 1(Open)
Chapter 2(Open)
Chapter 3(Open)
Chapter 4(Open)
Chapter 5(Open)
Chapter 6(Open)
Chapter 7(Open)
Chapter 8(Open)
Chapter 9(Open)
Chapter 10(Open)
Chapter 11(Open)
Chapter 12(Open)
Chapter 13(Open)
Chapter 14(Open)
Chapter 15(Open)
Chapter 16(Open)
Download complete book

Sunday, 26 February 2023

Pointers in C Programming

In this post we are going to discuss:

  • Variables and their Memory Addresses
  • Pointers
  • Storing Address of Pointer
  • Pointer Arithmetic

Variables:

Variables are the names given to memory locations. Every variable is stored at a unique memory location, it is very difficult to remember each location's address, so each location is assigned a name which can be easily remembered. Suppose we declare an integer variable as follows:

int a=10; //a is the name of memory location where constant 10 is stored

Let us say address of variable a is 2004, so we can represent it as:
    

                                                          

Here we can say that

Value of a is 10 or Value at Address 2004 is 10

Address of a is 2004


Pointers : These are special variables which are used to store address of another variable. In C language we can declare a pointer using following syntax:

data-type *ptr;

Here data-type is any built-in or user defined data type.

Asterisk (*) symbol is used to tell compiler that we are declaring a pointer.

ptr is the name of pointer variable.

Note: Asterisk (*) symbol has multiple uses in C language. It is used as symbol for multiplication, it is used to declare pointers and it is also used to find value at given address,

Two Special Operators:

Asterisk (*) : Value at Address Operator

Ampersand (&) : Address of Operator

Asterisk (*) is used to find value at an address. Address is usually stored in a pointer variable.

Ampersand (&) is used to refer to address of a variable or a function.

Pointer Example:


Output :

Value of a is 10
Address of a is 2004
Value of a is 10
Address of a is 2004

In above code we have declared an int variable a with initial value 10, ptr is a pointer variable of type int.

Note: data-type of pointer variable and of variable who's address is to be stored in pointer must be same. 

In our example ptr is used to store address of a i.e 2004. (data-type of a and ptr is same i.e. int)

          

Suppose address of a is 2004 and of ptr is 3002.

In first printf we are printing value of a

In second printf will display address of a i.e. 2004,

In 3rd printf we are printing value at address stored in ptr i.e. value at 2004 which is also 10

In last printf it will simply display the address stored in ptr i.e. 2004.

printf("%u",&ptr);

It will display address of ptr which is 3002.

printf("%d",*(*(&ptr)));

Let us evaluate above printf: &ptr is 3002 so it becomes *(*3002), now value at 3002 is 2004 again 

value at 2004 is 10. So it will print value of a which is 10.

How to store address of a pointer?

In case we want to store address of a pointer we need a double pointer. To declare a double pointer we need two asterisk (**) followed by pointer name. If we need to store address of double pointer we need triple pointer and so on.
Eg:
int a=10;
int *ptr1,**ptr2;
ptr1=&a;
ptr2=&ptr1;

Let us say address of a is 2004, address of ptr1 is 3002 and address of ptr2 is 4200.


If we refer to a it is 10.
If we refer to ptr1 it is 2004.
If we refer to ptr2 it is 3002.

Address of a i.e. &a is 2004.
Address of ptr1 i.e. &ptr1 is 3002.
Address of ptr2 i.e. &ptr2 is 4200.

Value at ptr1 i.e. *ptr1 is 10.
Value at ptr2 i.e. *ptr2 is 2004.
**ptr2  is **3002 which is *2004 which is 10.

Pointer Arithmetic

Like normal variables pointer variables may also be used with some operators in C. Here are some important points for pointer arithmetic:

Integer can be added or subtracted in a pointer variable i.e. we can add or subtract integers in pointer variables using + (plus) and - (minus) operators.

We cannot apply *, /, % operators to pointer variable i.e. we cannot multiply/divide/modulus integers with pointer variables.

Eg: 

int a=10;

int *ptr;




printf("%u",ptr+2);//valid two location after 2004

It will print ptr + 2 * sizeof(int) i.e. ptr+4 (if int takes 2 bytes) (2008)

printf("%u",ptr-3);//valid 3 locations before 2004

It will print ptr - 3 * sizeof(int) i.e. ptr-6 (if int takes 2 bytes) (1998)

printf("%u",ptr*2);//It will generate error

printf("%u",ptr/2;)//It will generate error

printf("%u",ptr%4);//It will generate error



We cannot apply *, /, %, + to two pointer variables, i.e. we cannot multiply/divide/modulus/add two pointer variables. But two pointer variables can be subtracted.

Eg:

int *ptr1,*ptr2;

printf("%u",ptr1-ptr2);//Valid

printf("%u",ptr1+ptr2);//It will generate error

printf("%u",ptr1*ptr2);//It will generate error

printf("%u",ptr1/ptr2);//It will generate error

printf("%u",ptr1%ptr2);//It will generate error

Note: Pointer variables are used to store addresses of memory which can be related to addresses of some houses. Like we usually say that my house is located after or before 3 (or any other number) houses of some person. It means we usually add or subtract numbers from real house addresses. We also say that there is a difference of 2 (or any other number) houses in my house and his/her house.

Follow us on:




Sunday, 22 January 2023

Defining member functions in C++

 Defining member functions in C++

In C++ when we create a function inside a class it is known as member function. A member function in C++ can be defined in two different ways:

  • We can define function inside the class i.e. body of the member function will be provided inside class definition.

  • We can declare function inside the class i.e. its prototype will be declared inside the class definition but its definition i.e. its body will be defined outside the class.
If we know that our function definition is small in length, it does not contain any loops or any other complex statements then we may define member function inside the class. If a member function is defined inside a class then compiler may try it treat it as inline function. As a result all the advantages and drawbacks of inline functions will come along.

Example:


Output:

a = 10

b = 20

Click Here to Download Source Code

Another way is to give prototype of function inside the class i.e. function can be declared inside the class and its definition is provided outside the class. If we define a member function outside the class then we have to use scope resolution operator to differentiate it from functions of other classes.

Example:


Output:

a = 10

b = 20

Click Here to Download Source Code

In case we declare member function inside the class then location of its declaration will decide its visibility i.e. it is public, private or protected. In our case both member functions are declared in public section so they are public in nature.



#c++ #cpp #scoperesolution #memberfunctions #cplusplus #functions #definition #coding #codingmadeasy #codingmadeasy #clanguage #cprogramming #java #javaprogramming #datastructure #algorithms #complexity #coding #codeforfun #codinglife #coderlife #programming #program #computerscience #it #technology #ugc #gate #gate #nta #net

Wednesday, 11 January 2023

Scope Resolution (::) Operator in C++

 Scope Resolution (::) Operator in C++

Scope Resolution Operator in C++ is denoted by two consecutive colon (::) symbols. The common uses of Scope Resolution Operator:

  • It is used to access global variable in a program if it has same name as of a local variable in a function.
  • It is used to define static variables outside class.
  • It is used to access static member functions and static data members outside class.
  • It is used to define a member function outside the class.
  • It is used to resolve ambiguity in case of multiple inheritance.
  • It is used to refer data members and member functions of a nested class.
  • It is used to access classes and objects from a namespace.

Now we will discuss each of the above uses one by one with suitable examples.
  • If we declare a global variable in a C++ program and it has same name as of some local variable in a function then by default local variable is accessed & it hides the visibility of global variable.
Example:


The above code will display "a = 50".
Now to make global variable visible in main function we will use scope resolution operator:


The above code will display "a = 50" and "a = 100" in next line.



  • Static variables declared within a class must also be defined outside the class, for this scope resolution operator is used.
  • Scope resolution operator is also used to access static members i.e. static data members (class variables) and static member functions outside the class. Example:
In this example we have declared static variable "a" inside class and it also has been defined outside the class using scope resolution operator with initial value 10.




In main function scope resolution operator is also used to access member function "display()" as well as class variable (static variable) "a".  Program will display "a = 10" and "a = 11" as output.


  • In C++ we can define member functions inside as well as outside the class. If member function is to defined outside the class then its declaration must be provided inside the class and definition must begin with identity label using class name and scope resolution operator like:
return-type class-name::function-name(parameter list)
{
//body
}
I will explain this with help of example given below:


In class abc show function is declared inside but its definition is given outside the class using scope resolution operator. class-name::(abc::) is identity label which used to differentiate same name methods of different classes.


  • In case of multiple inheritance if more than one base class has same name member function, then the the derived class will have definition of both the functions but when one of the function is called then an error will be there. (Ambiguity in multiple inheritance)
To resolve ambiguity we use scope resolution operator. Example:


Output:
    Class A
    Class B
    Class C

In above example classes A,B & C has function fxn(). Class C is derived class which inherit both A &B. So, in class C we have three definitions of fxn() will cause ambiguity when called by the object of Class C i.e. ob, to resolve this we have used scope resolution operator as:
    ob.A::fxn();// it will call fxn() of Class A
    ob.B::fxn();// it will call fxn() of Class B
    ob.C::fxn();// it will call fxn() of Class C

  • Sometime we define a class within another class which is known as nested class, to declare object of the nested class we use scope resolution operator.
Example:

Output:
Outer Class A
Inner Class B

We have used scope resolution operator to declare object of class B which is nested inside class A.

  • We can use objects from a namespace without using namespace declaration. It can be achieved with help of scope resolution operator.





#scoperesolutionoperator #nestedclass #staticclass #cpp #multipleinheritance #inheritance #local #global
#auto #break #case #char
#const #continue #default #do
#double #else #enum #extern
#float #for #goto #if
#int #long #register #return
#short #signed #sizeof #static
#struct #switch #typedef #union
#unsigned #void #volatile #while

#Keywords  #Identifier
#Variables  #Constants
#datatypes
#Input #output #functions
#Operators