he's a University Distinguished Professor and the holder of the College of Engineering Chair in Computer Science at TexasI designed and implemented the C++ programming language. To make C++ a stable and up-to-date base for real-world software development, I stuck with its ISO standards effort for 20+ years (so far). A and m University;this content is boggerised by sachin .pb contact me[...]

games for PlayStation are programmed in C++, since the PlayStation Dev Kit are in C++,XBox game are programmed in C#, using DirectX and XNA ... Computer games can be programmed even by flash, java, ..etc Good programming skills are required, but a complex game requires alot of specialized developer of different specialties from Graphics\Music\Animations to Artificial Intelligence, Game Platforms provide a Dev Kit, that you can download and use ... Apparently you need to use GLM to use OpenGL and C++.

why do we consider c++ as a top language???... becasue itz the most toughest danm language ever created so if we can understand c++ ..,,, then the programing language like java,c#,python .net etc.. becomes very easy peicey language... so if u understand c++ u are the man,...

this is a simple program created my me ..see how fun this is u can also try these once u know all the basic[...]

Wednesday, May 28, 2014

Function overloading

Posted by Unknown On 6:35 AM No comments

Function overloading -  it is a feature of c++ that allows us to create multiple functions(many functions) under the same name but the only thing is that they want to have different parameters



#include<iostream>
using namespace std;
void print (int x)
{
cout<<"the integer no is"<<x;
}
void print (float x)
{
cout<<" the float number is "<<x;
}
int main()
{
int a;
float b;
a=45;
b=5.55;
print(a);
print(b);
return 0;
}

here we are using the same function name for the 2 different functions. but the parameter of the 1st function is the type int  .  the parameter of the 2nd function is foat. .
so when we return a value from the main it checks its integer type and passes it to the desired function which is having the same parameter

Tuesday, September 17, 2013

Posted by Unknown On 5:03 AM No comments

Static Stack

#include<iostream.h>
#include<conio.h>
#define size 4

class stack
{
     int data[size];
     int top;
public:
     stack()
     {
          top=-1;
     }
     void push();
     void pop();
     void display();
};
void stack::push()
{
     if(top==size-1)
     {
          cout<<"\nStack is full";
          return;
     }
     else
     {
          top++;
          cout<<"Enter Data : ";
          cin>>data[top];
     }
}
void stack::pop()
{
     if(top==-1)
          cout<<"\n Stack is empty";
     else
     {
          cout<<data[top]<<"deleted "<<endl;
          top--;
     }
}
void stack::display()
{
     int t=top;
     while(t>=0)
     {
          cout<<data[t]<<endl;
          t--;
     }
}
void main()
{
     stack st;
     int ch;
     do
     {
          cout<<"\n1. Push\n2. Pop\n3. Display \n4.Quit\nEnter Choice(1-4) ";
          cin>>ch;
          switch(ch)
          {
               case 1: st.push();break;
               case 2: st.pop();break;
               case 3: st.display();
          }
     }while(ch!=4);
}
                                                                                                   

Wednesday, September 11, 2013

Pointer

Posted by Unknown On 5:34 AM No comments

Pointer

C++ Memory Map

Once a program is compiled, C++ creates four logically distinct regions of memory:
Code Area : Area to hold the compiled program code
Data Area : Area to hold global variables
Stack Area : Area to hold the return address of function calls, argument passed to the functions, local variables for functions and the current state of the CPU.
Heap : Area from which the memory is dynamically allocated to the program.

Accessing address of a variable

Computer’s memory is organized as a linear collection of bytes. Every byte in the computer’s memory has an address. Each variable in program is stored at a unique address. We can use address operator & to get address of a variable:
            int num = 23;
            cout << &num;       // prints address in hexadecimal

POINTER

A pointer is a variable that holds a memory address, usually the location of another variable in memory.
Defining a Pointer Variable
            int *iptr;
iptr can hold the address of an int
Pointer Variables Assignment:
 int num = 25;
 int *iptr;
 iptr = &num;
Memory layout
pointer memory map
To access num using iptr and indirection operator *
     cout << iptr;        // prints 0x4a00
     cout << *itptr;     // prints 25     
Similary, following declaration shows:
char *cptr;
float *fptr;
cptr is a pointer to character and fptr is a pointer to float value.

Pointer Arithmetic

Some arithmetic operators can be used with pointers:
   - Increment and decrement operators ++, --
    - Integers can be added to or subtracted from 
      pointers using the operators +, -, +=, and -=
Each time a pointer is incremented by 1, it points to the memory location of the next element of its base type. 
If “p” is a character pointer then “p++” will increment “p” by 1 byte.
If “p” were an integer pointer its value on “p++” would be incremented by 2 bytes.

Pointers and Arrays

Array name is base address of array
            int vals[] = {4, 7, 11};
            cout << vals;       // displays 0x4a00
            cout << vals[0]; // displays 4
Lets takes an example:
int arr[]={4,7,11};
int *ptr = arr;
What is ptr + 1?          
It means (address in ptr) + (1 * size of an int)
cout << *(ptr+1); // displays 7
cout << *(ptr+2); // displays 11
Array Access
Array notation  arr[i]  is equivalent to the pointer notation  *(arr + i)
Assume the variable definitions
   int arr[]={4,7,11}; 
   int *ptr = arr;
Examples of use of ++ and --
   ptr++; // points at 7
   ptr--; // now points at 4

Character Pointers  and Strings

Initialize to a character string.
char* a = “Hello”; 
a is pointer to the memory location where  ‘H’ is stored. Here “a” can be viewed as a character array of size 6, the only difference being that a can be reassigned another memory location.
 char* a = “Hello”;
 a          gives address of ‘H’
*a        gives ‘H’
a[0]      gives ‘H’
a++      gives address of ‘e’
*a++     gives ‘e’

Pointers as Function Parameters

A pointer can be a parameter. It works like a reference parameter to allow change to argument from within function
Pointers as Function Parameters
       void swap(int *x, int *y)
       {    
          int temp;
                              temp = *x;
                              *x = *y;
                              *y = temp;
       }
      swap(&num1, &num2);
Pointers to Constants and Constant Pointers
Pointer to a constant: cannot change the value that is pointed at
Constant pointer: address in pointer cannot change once pointer is initialized

Pointers to Structures

We can create pointers to structure variables
            struct Student {int rollno; float fees;};
            Student stu1;
            Student *stuPtr = &stu1;
            (*stuPtr).rollno= 104;
-or-
Use the form ptr->member:
            stuPtr->rollno = 104;

Static allocation of memory

In the static memory allocation, the amount of memory to be allocated is predicted and preknown. This memory is allocated during the compilation itself. All the declared variables declared normally, are allocated memory statically.

Dynamic allocation of memory

In the dynamic memory allocation, the amount of memory to be allocated is not known. This memory is allocated during run-time as and when required. The memory is dynamically allocated using new operator.

Free store

Free store is a pool of unallocated heap memory given to a program that is used by the program for dynamic allocation during execution.

Dynamic Memory Allocation

We can allocate storage for a variable while program is running by using new operator
To allocate memory of type integer
int *iptr=new int;
To allocate array
double *dptr = new double[25];
To allocate dynamic structure variables or objects
Student sptr = new Student;    //Student is tag name of structure

Releasing Dynamic Memory

Use delete to free dynamic memory
delete iptr; 
To free dynamic array memory
delete [] dptr; 
To free dynamic structure
delete Student;

Memory Leak

If the objects, that are allocated memory dynamically, are not deleted using delete, the memory block remains occupied even at the end of the program. Such memory blocks are known as orphaned memory blocks. These orphaned memory blocks when increase in number, bring adverse effect on the system. This situation is called memory leak

Self Referential Structure

The self referential structures are structures that include an element that is a pointer to another structure of the same type.
struct node 
{
  int data;
  node* next;
}

Tuesday, September 10, 2013

Basic Operation On Text File In C++

Posted by Unknown On 6:02 AM No comments

Basic Operation On Text File In C++

Program to write in a text file

#include<fstream.h>
int main()
{
     ofstream fout;
     fout.open("out.txt");
     char str[300]="Time is a great teacher but unfortunately it kills all its pupils. Berlioz";
     fout<<str;
     fout.close();
     return 0;
}

Program to read from text file and display it

#include<fstream.h>
#include<conio.h>
int main()
{
     ifstream fin;
     fin.open("out.txt");
     char ch;
     while(!fin.eof())
     {
          fin.get(ch);
          cout<<ch;
     }
     fin.close();
     getch();
     return 0;
}

Program to count number of characters.

#include<fstream.h>
#include<conio.h>
int main()
{
     ifstream fin;
     fin.open("out.txt");
     clrscr();
     char ch; int count=0;
     while(!fin.eof())
     {
          fin.get(ch);
          count++;
     }
     cout<<"Number of characters in file is "<<count;
     fin.close();
     getch();
     return 0;
}

Program to count number of words

#include<fstream.h>
#include<conio.h>
int main()
{
     ifstream fin;
     fin.open("out.txt");
     char word[30]; int count=0;
     while(!fin.eof())
     {
          fin>>word;
          count++;
     }
     cout<<"Number of words in file is "<<count;
     fin.close();
     getch();
     return 0;
}

Program to count number of lines

#include<fstream.h>
#include<conio.h>
int main()
{
     ifstream fin;
     fin.open("out.txt");
     char str[80]; int count=0;
     while(!fin.eof())
     {
          fin.getline(str,80);
          count++;
     }
     cout<<"Number of lines in file is "<<count;
     fin.close();
     getch();
     return 0;
}

Program to copy contents of file to another file.

#include<fstream.h>
int main()
{
     ifstream fin;
     fin.open("out.txt");
     ofstream fout;
     fout.open("sample.txt");
     char ch;
     while(!fin.eof())
     {
          fin.get(ch);
          fout<<ch;
     }
     fin.close();
     return 0;
}

Basic Operation On Binary File In C++

class student
{
            int admno;
            char name[20];
public:
          void getdata()
          {
                     cout<<"\nEnter The admission no. ";
                     cin>>admno;
                     cout<<"\n\nEnter The Name of The Student ";
                     gets(name);
          }
          void showdata()
          {
                     cout<<"\nAdmission no. : "<<admno;
                     cout<<"\nStudent Name : ";
                     puts(name);
          }
          int retadmno()
          {
                     return admno;
          }
};

function to write in a binary file

void write_data()
{
          student obj;
          ofstream fp2;
          fp2.open("student.dat",ios::binary|ios::app);
          obj.getdata();
          fp2.write((char*)&obj,sizeof(obj));
          fp2.close();
}

function to display records of file

void display()
{
          student obj;
          ifstream fp1;
          fp1.open("student.dat",ios::binary);
          while(fp1.read((char*)&obj,sizeof(obj)))
          {
                     obj.showdata();
          }
}
          fp.close();
}

Function to search and display from binary file

void search (int n)
{
          student obj;
          ifstream fp1;
          fp1.open("student.dat",ios::binary);
          while(fp1.read((char*)&obj,sizeof(obj)))
          {
                     if(obj.retadmno()==n)
                                obj.showdata();
          }
          fp1.close();
}

Function to delete a record

void deleterecord(int n)
{
          student obj;
          ifstream fp1;
          fp1.open("student.dat",ios::binary);
          ofstream fp2;
          fp2.open("Temp.dat",ios::out|ios::binary);
          while(fp1.read((char*)&obj,sizeof(obj)))
          {
                      if(obj.retadmno!=n)
                                    fp2.write((char*)&obj,sizeof(obj));
          }
          fp1.close();
          fp2.close();
          remove("student.dat");
          rename("Temp.dat","student.dat");
}

Function to modify a record

void modifyrecord(int n)
{
          fstream fp;
          student obj;
          int found=0; 
          fp.open("student.dat",ios::in|ios::out);
          while(fp.read((char*)&obj,sizeof(obj)) && found==0)
          {
                     if(obj.retadmno()==n)
                     {
                              obj.showdata();
                              cout<<"\nEnter The New Details of student";
                              obj.getdata();
                              int pos=-1*sizeof(obj);
                              fp.seekp(pos,ios::cur);
                              fp.write((char*)&obj,sizeof(obj));
                              found=1;
                    }
          }
          fp.close();
}

Data File Handling In C++

Posted by Unknown On 5:59 AM No comments

Data File Handling In C++

File. The information / data stored under a specific name on a storage device, is called a file.
Stream. It refers to a sequence of bytes.
Text file. It is a file that stores information in ASCII characters. In text files, each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character. When this EOL character is read or written, certain internal translations take place.
Binary file. It is a file that contains information in the same format as it is held in memory. In binary files, no delimiters are used for a line and no translations occur here.    

Classes for file stream operation

ofstream: Stream class to write on files
ifstream: Stream class to read from files
fstream: Stream class to both read and write from/to files.

Opening a file

OPENING FILE USING CONSTRUCTOR
ofstream fout(“results”);    //output only
ifstream fin(“data”);  //input only
OPENING FILE USING open()
Stream-object.open(“filename”, mode)
      ofstream ofile;
      ofile.open(“data1”);
      
      ifstream ifile;
      ifile.open(“data2”);
File mode parameterMeaning
ios::appAppend to end of file
ios::atego to end of file on opening
ios::binaryfile open in binary mode
ios::inopen file for reading only
ios::outopen file for writing only
ios::nocreateopen fails if the file does not exist
ios::noreplaceopen fails if the file already exist
ios::truncdelete the contents of the file if it exist
All these flags can be combined using the bitwise operator OR (|). For example, if we want to open the file example.bin in binary mode to add data we could do it by the following call to member function open():
fstream file
file.open ("example.bin", ios::out | ios::app | ios::binary);

Closing File

   fout.close();
   fin.close();

INPUT AND OUTPUT OPERATION

put() and get() function
the function put() writes a single character to the associated stream. Similarly, the function get() reads a single character form the associated stream.
example :
file.get(ch);
file.put(ch);
write() and read() function
write() and read() functions write and read blocks of binary data.
example:
file.read((char *)&obj, sizeof(obj));
file.write((char *)&obj, sizeof(obj));

ERROR HANDLING FUNCTION

FUNCTIONRETURN VALUE AND MEANING
eof()returns true (non zero) if end of file is encountered while reading; otherwise return false(zero)
fail()return true when an input or output operation has failed
bad()returns true if an invalid operation is attempted or any unrecoverable error has occurred.
good()returns true if no error has occurred.

 

File Pointers And Their Manipulation

All i/o streams objects have, at least, one internal stream pointer: 
ifstream, like istream, has a pointer known as the get pointer that points to the element to be read in the next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to the location where the next element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is itself derived from both istream and ostream). 

These internal stream pointers that point to the reading or writing locations within a stream can be manipulated using the following member functions:
seekg()moves get pointer(input) to a specified location
seekp()moves put pointer (output) to a specified location
tellg()gives the current position of the get pointer
tellp()gives the current position of the put pointer
 
The other prototype for these functions is:
seekg(offset, refposition ); 
seekp(offset, refposition );
The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter refposition. The refposition takes one of the following three constants defined in the ios class.
ios::beg          start of the file
ios::cur          current position of the pointer
ios::end          end of the file
example:
file.seekg(-10, ios::cur);

Inheritance

Posted by Unknown On 5:58 AM No comments

Inheritance

Inheritance:It is the capability of one class to inherit properties from another class.
Base Class: It is the class whose properties are inherited by another class. It is also called Super Class.
Derived Class:It is the class that inherit properties from base class(es).It is also called Sub Class.

Forms Of Inheritance

Single Inheritance: It is the inheritance hierarchy wherein one derived class inherits from one base class.
Multiple Inheritance:It is the inheritance hierarchy wherein one derived class inherits from multiple base class(es)
Hierarchical Inheritance: It is the inheritance hierarchy wherein multiple subclasses inherits from one base class.
Multilevel Inheritance: It is the inheritance hierarchy wherein subclass acts as a base class for other classes.
Hybrid Inheritance:The inheritance hierarchy that reflects any legal combination of other four types of inheritance.

Visibility Mode:It is the keyword that controls the visibility and availability of inherited base class members in the derived class.It can be either private or protected or public.
Private Inheritance:It is the inheritance facilitated by private visibility mode.In private inheritance ,the protected and public members of base class become private members of the derived class.
Public Inheritance:It is the inheritance facilitated by public visibility mode.In public inheritance ,the protected  members of base class become protected members of the derived class and public members of the base class become public members of derived class.;
Protected Inheritance:It is the inheritance facilitated by protected visibility mode.In protected inheritance ,the protected and public members of base class become protected members of the derived class.
Base Class VisibilityDerived class visibility
Public derivationPrivate derivationProtected derivation
PrivateNot inheritedNot inheritedNot inherited
ProtectedProtectedPrivateProtected
PublicPublicPrivateProtected

Containership:When a class contains objects of other class types as its members, it is called containership.It is also called containment,composition, aggregation.

Execution of base class constructor

Method of inheritaceOrder of execution
class B : public A { };A(); base constructor 
B(); derived constructor
class A : public B, public CB();base (first) 
C();base (second) 
A();derived constructor
When both derived and base class contains constructors, the base constructor is executed first and then the constructor in the derived class is executed.  In case of multiple inheritances, the base classes are constructed in the order in which they appear in the declaration of the derived class.

Overriding of method(function) in inheritance

We may face a problem in multiple inheritance, when a function with the same name appears in more than one base class. Compiler shows ambiguous error when derived class inherited by these classes uses this function.     
We can solve this problem, by defining a named instance within the derived class, using the class resolution operator with the function as below :
class P : public M, public N        //multiple inheritance 
{
 public :
   void display()  //overrides display() of M and N
   { 
      M::display() 
   }
};
we can now used the derived class as follows :
 void main()
{
  P obj;
  obj.display();
}

Virtual Base Class

Multipath inheritance may lead to duplication of inherited members from a grandparent base class. This may be avoided by making the common base class a virtual base class. When a class is made a virtual base class, C++ takes necessary care to see that only one copy of that class is inherited.
class A
{
 ....
 ....
};
class B1 : virtual public A

 ....
 ....
};
class B2 : virtual public A
{
 ....
 ....
};
class C : public B1, public B2
{
 ....   // only one copy of A
 ....   // will be inherited
};

Constructor and Destructor

Posted by Unknown On 5:54 AM No comments

Constructor and Destructor

Constructor

It is a member function having same name as it’s class and which is used to initialize the objects of that class type with a legel initial value. Constructor is automatically called when object is created.

Types of Constructor

Default Constructor-: A constructor that accepts no parameters is known as default constructor. If no constructor is defined then the compiler supplies a default constructor.
student :: student()
{
     rollno=0; 
     marks=0.0; 
}
Parameterized Constructor -: A constructor that receives arguments/parameters, is called parameterized constructor.

student :: student(int r)
{
     rollno=r; 
}
Copy Constructor-: A constructor that initializes an object using values of another object passed to it as parameter, is called copy constructor. It creates the copy of the passed object.

student :: student(student &t)
{
     rollno = t.rollno; 
}
There can be multiple constructors of the same class, provided they have different signatures.

Destructor

A destructor is a member function having sane name as that of its class preceded by ~(tilde) sign and which is used to destroy the objects that have been created by a constructor. It gets invoked when an object’s scope is over.

~student() { }
Example : In the following program constructors, destructor and other member functions are defined inside class definitions. Since we are using multiple constructor in class so this example also illustrates the concept of constructor overloading
#include<iostream.h>

class student //specify a class
{
  private :
    int rollno; //class data members
    float marks; 
  public:
    student() //default constructor
    {
       rollno=0;
       marks=0.0;
    }
    student(int r, int m) //parameterized constructor
    {
       rollno=r;
       marks=m;
    }
    student(student &t) //copy constructor
    {

       rollno=t.rollno;
       marks=t.marks;
    }
    void getdata() //member function to get data from user
    {
       cout<<"Enter Roll Number : ";
       cin>>rollno;
       cout<<"Enter Marks : ";
       cin>>marks;
    }
    void showdata() // member function to show data
    {
       cout<<"\nRoll number: "<<rollno<<"\nMarks: "<<marks;
    }
    ~student() //destructor
    {} 
};

int main()
{
    student st1; //defalut constructor invoked
    student st2(5,78); //parmeterized constructor invoked
    student st3(st2); //copy constructor invoked
    st1.showdata(); //display data members of object st1
    st2.showdata(); //display data members of object st2
    st3.showdata(); //display data members of object st3
    return 0;
}

Monday, September 9, 2013

Class & Objects

Posted by Unknown On 9:30 AM No comments

Class & Objects

The mechanism that allows you to combine data and the function in a single unit is called a class. Once a class is defined, you can declare variables of that type. A class variable is called object or instance. In other words, a class would be the data type, and an object would be the variable. Classes are generally declared using the keyword class, with the following format:
class class_name 
{
   private:
      members1;
   protected:
      members2;
   public:
      members3;
};
Where class_name is a valid identifier for the class. The body of the declaration can contain members, that can be either data or function declarations, The members of a class are classified into three categories: private, public, and protected. Private, protected, and public are reserved words and are called member access specifiers. These specifiers modify the access rights that the members following them acquire.
private members of a class are accessible only from within other members of the same class. You cannot access it outside of the class.
protected members are accessible from members of their same class and also from members of their derived classes. 
Finally, public members are accessible from anywhere where the object is visible.
By default, all members of a class declared with the class keyword have private access for all its members. Therefore, any member that is declared before one other class specifier automatically has private access.
Here is a complete example :
class student 
{
  private :
    int rollno;
    float marks; 
  public:
    void getdata()
    {
       cout<<"Enter Roll Number : ";
       cin>>rollno;
       cout<<"Enter Marks : ";
       cin>>marks;
    }
    void displaydata()
    {
       cout<<"Roll number : "<<rollno<<"\nMarks : "<<marks;
    }
};

Object Declaration

Once a class is defined, you can declare objects of that type. The syntax for declaring a object is the same as that for declaring any other variable. The following statements declare two objects of type student:
student st1, st2;

Accessing Class Members

Once an object of a class is declared, it can access the public members of the class.
st1.getdata();

Defining Member function of class

You can define Functions inside the class as shown in above example. Member functions defined inside a class this way are created as inline functions by default. It is also possible to declare a function within a class but define it elsewhere. Functions defined outside the class are not normally inline.
When we define a function outside the class we cannot reference them (directly) outside
of the class. In order to reference these, we use the scope resolution operator, ::
(double colon). In this example, we are defining function getdata outside the class:
void student :: getdata()
{
     cout<<"Enter Roll Number : ";
     cin>>rollno;
     cout<<"Enter Marks : ";
     cin>>marks;
}
The following program demostrates the general feature of classes. Member function initdata() is defined inside the class. Member funcitons getdata() and showdata() defined outside the class.
class student //specify a class
{
  private :
    int rollno; //class data members
    float marks; 
  public:
    void initdata(int r, int m)
    {
       rollno=r;
       marks=m;
    }
    void getdata(); //member function to get data from user
    void showdata();// member function to show data
};

void student :: getdata()
{
    cout<<"Enter Roll Number : ";
    cin>>rollno;
    cout<<"Enter Marks : ";
    cin>>marks;
}

void student :: showdata()
{
    cout<<"Roll number : "<<rollno<<"\nMarks : "<<marks;
}

int main()
{
    student st1, st2; //define two objects of class student
    st1.initdata(5,78); //call member function to initialize
    st1.showdata();
    st2.getdata(); //call member function to input data
    st2.showdata(); //call member function to display data
    return 0;
}