on Leave a Comment

C++ reference variables

C++ introduces some new concepts that were not previously available in C language. One of the concept is reference variable. A reference variable is used to store the reference of an ordinary variable.

Syntax of Reference Variable:

data-type &reference-name=variable-name

Example:

int a=10;
int &b=a;

Here a is int type variable, and b is reference variable storing the reference of variable a. Reference variable b is another name of variable a.

The statements 

cout<<a;
cout<<b;

both will print 10.

The statement

a=a+10;

will change the value of both a and b. 

The statement

b=b+10;

will change the value of both a and b.

Here, & is not an address of operator. The int & means reference to int.

Following examples are legal:

int a[5];
int &b=a[5];
int x;
int *y=&x;
int &z=*y;
int &p=10;
char &c='\n';

An application of reference variables is in passing arguments to functions

int add(int &x,int &y);
int main()
{
    int a=10;
    int b=20;
    int c;
    c=add(a,b);
    cout<<"Sum is "<<c;
}
int add(int &x,int &y)
{
    return x+y;
}

Here, we are passing references of a and b to x and y. Then, sum of x and y is assigned to c.

Important points about reference variable

Reference variable must be initialized during declaration.

Reference variable can be initialized with already declared variable.

Declaration of reference variable is always preceded with ‘&’ symbol (Here it is not address of operator).

Reference variable cannot be updated till the end of program.

Reference variable can only store the reference of variable of same data type. Example, float type reference variable can only store the reference of float type variable. 
on Leave a Comment

C++ Static Members of Class

Static is a keyword in C++. Static keyword is used to give some more characteristic of a variable or function. Concept of static local variable in C++ is similar to C language. But C++ has some new concepts of static member variable and static member functions that is not available in C language.

Objective

  1. Static local variable
  2. Static member variable
  3. Static member functions
STATIC LOCAL VARIABLE


Static local variable can be defined in a function using static keyword. The initial value of static local variable is by default zero. The scope of static local variable is throughout the lifetime of program.

For example,

#include<iostream>
using namespace std;
void fun();
int main()
{
    fun();
    return 0;
}
void fun()
{
    static int bh;              //Static local variable declaration
    cout<<"Value of bh is "<<bh;
}

OUTPUT:

Value of bh is 0

Here the initial value of bh is zero. Whereas the value of non-static variable is garbage value. Static keyword is necessary to declare a variable as static.

STATIC MEMBER VARIABLE

Static member variables are declared inside the class body. Static member variable are also known as class member variable. Declaration of static member variable is written inside the class but definition of static member variable is written outside the class. Static member variables do not belong to any separate object, but they belong to the whole class. Only one copy of static member variable is made for the whole class. Objects can use the static member variable.

Simple program of understand static member variable

#include<iostream>
using namespace std;
class B
{
    static int a;                      //static member variable declaration
    int b;
public:
    void setdata(int x)
    {
        b=x;
    }
};
int B:: a;                                //static member variable definition
int main()
{
    B obj;
    return 0;
}

Here a is a static member variable. As you can see above declaration of static member variable is written inside the class but definition of static member variable is written outside the class. Static member variable can be accessed using static member function.

If we declare static member variable as public inside the class. Then we can access static member variable using object and class name in main function. For example,

#include<iostream>
using namespace std;
class B
{
    public:
    static int a;
    int b;
public:
    void setdata(int x)
    {
        b=x;
    }
};
int B:: a;
int main()
{
    B obj;
    cout<<obj.a;
    cout<<endl;
    cout<<B::a;
    return 0;
}

OUTPUT:

0
0

STATIC MEMBER FUNCTION

Static member function is defined using static keyword. Static member function is usually used to access static member variable. Static member functions can be called by objects or by class.

An example to understand static member functions

#include<iostream>
using namespace std;
class B
{
    static int A;
    int b;
public:
    void setdata(int x)
    {
        b=x;
    }
    static void setA(int x)
    {
        A=x;
    }
    static void putA()
    {
        cout<<"\n"<<A;
    }
};
int B:: A;
int main()
{
    B obj;
    obj.setA(5);  //Access using object
    obj.putA();  
    B::setA(10);  //Access using class
    B::putA();
    return 0;
}

OUTPUT:

5
10

You can access static member function by either class name or object name. 
on Leave a Comment

Tokens in C++ | Types of Tokens in C++

Token is a smallest individual unit in a program. C++ program is written using these tokens, white spaces and syntax of C++ language. C++ tokens are similar to C tokens but some modifications is added in C++ tokens.

Types of Tokens in C++

Keywords
Identifiers
Constants
Variables
Operators

KEYWORDS

Keywords are reserved words and keywords cannot be used as variables or any user-defined functions names. Keywords are predefined words and their meaning is already known to the compiler. Many C++ language keywords are similar to C language keywords. ANSI C++ committee has added some more keywords in C++.

For example
int a;

Here int is a keyword. Meaning of int is already known to compiler.

IDENTIFIERS

The names taken by the programmer for creating variables, functions, arrays, classes etc are known as identifiers. C++ has its some own rules for naming identifiers. The rules for naming identifiers in C++ are
  • Only alphabets, digits and underscores are allowed for identifiers.
  • Digits are not allowed at the beginning of identifiers.
  • Uppercase and lowercase letters are distinct
  • Keywords cannot be used as variable’s name
All these rules are also same in C language.

CONSTANTS

Constants are fixed values that do not during the execution of a program. C++ support several types of literal constants.

Integer Constants

Integer constants are integers and they do not include decimal part. An integer constant can be either positive or negative.

Decimal integer constant (Base 10): Integer constants that do not start with 0. Example: 2, 67, -89 etc.

Octal integer constant (Base 8): Integer constants that starts with 0. Example: 089, 034 etc.

Hexadecimal integer constant (Base 16): Integer constants that starts with 0x or 0X. Example: 0XC

Floating Constants

Floating constants are also known as real constants. Floating constants contains decimal part. They can be either negative or positive. For example: 7.8, -2.1, 5.0, -1.0 etc.

Character Constants

Character constant is a single character enclosed by single quotes. A single character can be character constant. It can be letters, digits, operator and even space.

All these are character constant:
‘1’, ‘+’, ‘a’, ‘B’, ‘ ’

A character constant must contain only one character. For example, ‘-6’ is not character constant because it consists of two characters – and 6.

String constants

A string is a collection of characters. It is a sequence of character enclosed with double quotes.

For example: “PCTECHNICALPRO”

String is automatically appended by the special character called ‘\0’. ‘\0’ indicates end of string. So, the size of string is automatically increased by 1 bit because ‘\0’ is appended at the end of string.

PUNCTUATOR

Punctuators are most commonly used characters in C++.

For example: () {} [] , ; : * = #

() is used for function calls, arguments are placed in ().
{} indicates start and end of function body.
[] indicates single and multidimensional array subscript.
, is used to separate arguments in function declaration, definition and calling.
; is used to terminate statement.
: is used in conditional operator, scope resolution operator etc.
= is used for assigning values.
* is used in multiplication and pointer declaration.

# is used in pre-processor directives.

OPERATORS

Operator is used for manipulating an expression. 

C++ operators are:

Arithmetic Operators 
Increment / Decrement Operators
Relational Operators
Logical operators
Conditional Operators












    
on Leave a Comment

Basic Concepts of Object Oriented Programming

Object oriented programming is a guideline that ensures to program to be developed in more managed way. Object oriented programming removes all flaws that is encountered in procedural oriented programming. OOPs combines data and their related function together to ensure that it is not accessed by outside function. Object oriented programming focuses on data rather than procedure. 

Concepts of Object Oriented Programming

Object

Object is a run time entity. Object represents a real world entity such as place, person, car etc. Object is an instance of a class. Object holds apace in memory.

Syntax of object:

<class_name> <object-name>;

Assume, number is a class. If we write number obj1; in our program, then obj1 is a instance of class number, also known as variable of class number.

Class

In simple words, class is a description of an object. Class contains data and code related to an object.

Syntax of class: 

class <class_name>
{
       public:
       //Instance member functions and variable declarations

       private:
       //Instance member functions and variable declarations

       protected;
       //Instance member functions and variable declarations
};

Encapsulation

Encapsulation is a concept of placing all data and functions into a single unit called class. Encapsulation ensures that data is not accessible to outside function. Only those functions that are placed in class can access that data. The functions that are written inside the class, provides an interface between object's data and program.

Abstraction

Abstraction refers to hiding the background details and explanation of data and represents only essential information. In object oriented programming, class is used to achieve abstraction. A class hold all attributes of an object. These attributes are called data member. These attributes are hidden to outside world. Class uses the concept of data abstraction, so class is also known as Abstract Data Type (ADT).

Inheritance

Inheritance is a process by which new class is created from an existing class. New class acquires the properties of existing class. It provides the concept of hierarchical classification. Inheritance provides the concept of code reusability because we can add some new features to an existing class without modifying it. In inheritance, we derive new class from existing class.

Polymorphism

Polymorphism is the ability to use a function or an operator in many ways. For example, consider a function add, this function add two numbers. If this function can add also three or four number, this means add function is used in many ways, polymorphism is achieved.  
on Leave a Comment

Introduction to C++ language

C++ is an object-oriented programming language. C++ was developed by Bjarne Stroustrup at AT&T Bell Laboratories. In 1978, Bjarne Stroustrup was started working on the enhancement of C language. Bjarne Stroustrup was developed a programming language in 1979, called C with Classes. Bjarne Stroustrup was added many new concepts in C such as class concept and many other object-oriented concept. In 1983, C with Classes was further named C++.

Facts of C++

World first object-oriented programming language was Simula67. C++ was inspired by computer simulation language called Simula67.

Java is written in C++.

Major operating systems are written in C++.

C++ is the world 4th most used programming language.

Features of C++

C++ is a middle level language. Middle level language means both low level and high programs can be written in C++. Low level programming means machine dependent programming such as device driver programs and system software programs. High level programming includes application programs and these programs are independent to hardware’s.

C++ supports object-oriented paradigm.

In C++, three separate programming traditions can be allowed
  • Procedural programming like C
  • Object-oriented programming
  • Generic programming by C++ templates

Advantages of C++ language over C language

C++ is a super set C language

C++ programs can use existing C library files

C++ follows bottom up approach of programming language

In C++, a variable can be declared anywhere in a program.

Concept of class is not available in C language

Many other concepts such as virtual function, constructor, destructor etc is not available in C language


on Leave a Comment

Database Keys in DBMS

We must have database keys in DBMS to reduce redundancy in a relational database. Key is a key part of a relational database. Keys ensure that records do not repeat in a database. Keys help in identifying relationship between two tables.

SUPER KEY

Super key is a set of one or more attributes that is able to identifying each record or tuple uniquely. Roll numbers of student is able to identify each student uniquely, and then roll number is a super key of table student. Also combination of name and roll no is able to identify each student uniquely and then combination of name and roll no is super key. But only name of a student is not able to identify each student uniquely because name of two students can be same.

CANDIDATE KEY

Concept of super key is not sufficient because a super key may contain some additional attribute. A Candidate key solves this problem. A candidate key is a sub set of super key. All attributes of super key with no redundant value can be considered as candidate key. A candidate key can be one or more attributes that uniquely identifies each record.
An attribute can be candidate key if it follows following criteria
  • It must contain unique value
  • It must not contain NULL value
  • It must uniquely identify each row or record. 

PRIMARY KEY

A primary is chosen by database designer. A primary key is a candidate key that is most able to uniquely identify each row. In a relation, there may be more than one candidate key. But from that entire candidate keys only most appropriate key is chosen as primary key.  

ALTERNATIVE KEY OR SECONDARY KEY

The attributes that remains after selecting primary key from candidate key can be considered as alternative key. Alternative key is candidate keys – primary key.

FOREIGN KEY

The attributes or columns in a table that points to the primary key of another table is considered as foreign key. Foreign key establishes relationship between two tables.

on Leave a Comment

Entity Relationship Diagram Tutorial

ER diagram is entity relationship diagram. ER diagram represents relationship between entities and attributes.

Entities: Any real world object is called entity. Example of real world object is student, employee, customer etc. Entities can be either animate or inanimate.

Entity set: Entity set is a collection of entities of same type. For example, records of students in a class.

Entity instance: A specific individual entity in an entity set. For example, a particular student in a class.

Key attributes in entity: An attribute having unique value for entities in an entity set is key attribute. For example, roll no (attribute) is unique for every student (entity) in a class.

Weak type entity: An entity that has no key attributes is known as weak type entity. Weak type entity is also known as child or subordinate entity type. For example, a person with attributes name, gender and age has no key attribute. Name, gender and age of two people can be same.

Strong type entity: An entity having key attributes is known as strong type entity. Strong type entity is also known as regular entity. For example, student with roll no attribute is strong type entity. Roll no of two student cannot be same.

Relationship

In ER diagram, relationship is represented as diamond symbol. Entity is represented as rectangle symbol and attribute is represented as ellipse or oval symbol. Link between entities and attribute is represented by a line.

Binary relationship

Binary relationship represents relationship between entities.
Suppose entity A and entity B
One to one

Only one instance of entity A can be associated with only one instance of entity B.

er diagram relationship one to one


One to many

Only one instance of entity A can be associated with many instances of entity B.
er diagram relationship one to many


Many to one

Many instances of entity A can be associated with only one instance of entity B.
er diagram relationship many to one


Many to many

Many instances of entity A can be associated with many instances of entity B.
er diagram relationship many to many

on 12 comments

Advantages and Disadvantages of ER Model in DBMS

ER model is a logical representation of an enterprise data. ER model is a diagrammatic representation of logical structure of database.

ER model describes relationship among entities and attributes.

ER diagram is firstly developed by Peter Chen in 1976.

Advantages of ER Model

Conceptually it is very simple: ER model is very simple because if we know relationship between entities and attributes, then we can easily draw an ER diagram.

Better visual representation: ER model is a diagrammatic representation of any logical structure of database. By seeing ER diagram, we can easily understand relationship among entities and relationship.

Effective communication tool: It is an effective communication tool for database designer.

Highly integrated with relational model: ER model can be easily converted into relational model by simply converting ER model into tables.

Easy conversion to any data model: ER model can be easily converted into another data model like hierarchical data model, network data model and so on.

Disadvantages of ER Model

Limited constraints and specification

Loss of information content: Some information be lost or hidden in ER model

Limited relationship representation: ER model represents limited relationship as compared to another data models like relational model etc.

No representation of data manipulation: It is difficult to show data manipulation in ER model.

Popular for high level design: ER model is very popular for designing high level design


No industry standard for notation
on Leave a Comment

Data warehouse concept in DBMS

Data warehouse is also known as enterprise data. Data warehouse was first introduced by Bill Inmon in 1990. An organization needs the historical data for analyzing and reporting. These historical data helps an organization for better decision making about their organization.  Data warehouse contains historical and current data of organization. A data warehouse is subject-oriented, integrated, time variant and non-volatile collection of data. Large organizations have complex internal structure and their data is stored in different locations. For example, customer data and product data may be stored on different locations. But a corporate decision maker requires access of all these data at one place. Data warehouse is a solution of this problem.

Subject-oriented:  Subject-oriented means providing information about only subject rather than the other topic. For an organization subject can be products, sales, revenue and so on.

Integrated: A data warehouse contains data from mixed or heterogeneous sources.

Time-variant: A data warehouse is time variant because a particular data in data warehouse is valid at particular time.

Non-volatile: A data warehouse is kept separate from on-going operation database. In data warehouse previous data is not deleted when new data is added to data warehouse.

Advantages of Data Warehouse
  • Helps decision making for an organization
  • Cost effective decision making
  • Customer services enhances
  • Customer relationship enhances
  • Gives competitive advantages

Disadvantages of Data Warehouse
  • Implementation of data warehouse is very costly
  • High maintenance is required
  • Time consuming
  • Hidden problems

Characteristics of Data Warehouse
  • Multidimensional conceptual view
  • Generic dimensionality
  • Multiuser support
  • Flexible reporting
  • Accessibility of data from different sources
  • Initiative data manipulation
  • Consistent reporting performance

on Leave a Comment

Inline function in C++

Inline function is a concept in C++ that helps in reducing execution time of small functions. As we have already know that a function utilizes memory space but increases execution time, because of some tasks like saving registers, pushing arguments into the stack etc.

If function is large, then it is useful because it saves more memory space, but it also increases execution time. But comparison to the advantage of memory space, execution time may appear negligible.  

But if function is small, it saves less memory space and increasing execution time.

Inline function is a way of solving problems of small function. Inline function is a function that is expanded in a line when it is invoked. Compiler replaces function call statement into corresponding function code.

Syntax of defining inline function:

inline <return-type> <function-name> (<list of arguments>)
{
                function body
}

Example:

inline int add(int a, int b)
{
                return a+b;
}

PROGRAM:

inline function in c++


OUTPUT:

C++ inline function program output


It is important know that inline keyword is a request not a command. A compiler can ignore this request if the following conditions satisfy;
  • Function containing loop, switch and goto statements.
  • Function is recursive.
  • Function containing static variables.