Category: C++

Sample C++ Programs

The Whole File:- Sample C++

  • average.C
  • mult.C (multiplication using addition)
  • fastmult.C (better algorithm for multiplication)
  • more.C (count number of characters and lines in a file)
  • merge.C (merge sort)
  • grade.C
  • grade2.C
  • gcd.C (Euclid’s algorithm)
  • fib.C (Fibonacci numbers)
  • fact.C (factorial)
  • exp.C (exponentiation using multiplication)
  • commute.C (weird behavior from C++ compiler)
  • cin.C (more weird C++)
  • temp.C (convert temperatures)
  • sqrt.C (compute square roots using Newton’s method)
  • sphere.C (compute distance between cities)
  • prime.C (check if a number is prime)
  • prime2.C (faster but probabilistic way)
  • prime3.C (avoid stupid C++ behavior in case of overflow)
  • pp.C (understanding call-by-value and call-by-reference)
  • pp2.C (evaluating expressions with side-effects)
  • ref.C (example of pass-by-reference)
  • oddeven.C (lots of functions)
  • void.C (converting value-returning functions to void functions)
  • side.C (bad style, side-effects, value-returning)
  • sc.C (understanding scope)
  • life.C (understanding life time and scope)
  • object.C (understanding the concept of objects)

C++ Questions and Explanation

The Whole File:- C++ Questions and Explanation

1) class Sample
int *ptr;
Sample(int i)
ptr = new int(i);
delete ptr;
void PrintVal()
cout << “The value is ” << *ptr;
void SomeFunc(Sample x)
cout << “Say i am in someFunc ” << endl;
int main()
Sample s1= 10;

Say i am in someFunc
Null pointer assignment(Run-time error)

As the object is passed by value to SomeFunc  the destructor of the object is called when
the control returns from the function. So when PrintVal is called it meets up with ptr  that has
been freed.The solution is to pass the Sample object  by reference to SomeFunc:
void SomeFunc(Sample &x)
cout << “Say i am in someFunc ” << endl;
because when we pass objects by refernece that object is not destroyed. while returning from the

2) Which is the parameter that is added to every non-static member function when it is called?
‘this’ pointer

3) class base
int bval;
base(){ bval=0;}
class deri:public base
int dval;
deri(){ dval=1;}
void SomeFunc(base *arr,int size)
for(int i=0; i<size; i++,arr++)
int main()
base BaseArr[5];
deri DeriArr[5];

The function SomeFunc expects two arguments.The first one is a pointer to an array of
base class objects and the second one is the sizeof the array.The first call of someFunc calls it
with an array of bae objects, so it works correctly and prints the bval of all the objects. When
Somefunc is called the second time the argument passed is the pointeer to an array of derived
class objects and not the array of base class objects. But that is what the function expects to be
sent. So the derived class pointer is promoted to base class pointer and the address is sent to the
function. SomeFunc() knows nothing about this and just treats the pointer as an array of base
class objects. So when arr++ is met, the size of base class object is taken into consideration and
is incremented by sizeof(int) bytes for bval (the deri class objects have bval and dval as members
and so is of size >= sizeof(int)+sizeof(int) ).

4) class base
void baseFun(){ cout<<“from base”<<endl;}
class deri:public base
void baseFun(){ cout<< “from derived”<<endl;}
void SomeFunc(base *baseObj)
int main()
base baseObject;
deri deriObject;

from base
from base

As we have seen in the previous case, SomeFunc expects a pointer to a base class. Since
a pointer to a derived class object is passed, it treats the argument only as a base class pointer and
the corresponding base function is called.

****Many More****

C++ interview Q & A

click the link to download the whole file:

C++ interview Q & A

learn C++

The Whole File: C++ Tutorial

C++ Basics

  • C++ Home
  • C++ Overview
  • C++ Environment Setup
  • C++ Basic Syntax
  • C++ Comments
  • C++ Data Types
  • C++ Variable Types
  • C++ Variable Scope
  • C++ Constants/Literals
  • C++ Modifier Types
  • C++ Storage Classes
  • C++ Operators
  • C++ Loop Types
  • C++ Decision Making
  • C++ Functions
  • C++ Numbers
  • C++ Arrays
  • C++ Strings
  • C++ Pointers
  • C++ References
  • C++ Date & Time
  • C++ Basic Input/Output
  • C++ Data Structures

C++ Object Oriented

  • C++ Classes & Objects
  • C++ Inheritance
  • C++ Overloading
  • C++ Polymorphism
  • C++ Abstraction
  • C++ Encapsulation
  • C++ Interfaces

C++ Advanced

  • C++ Files and Streams
  • C++ Exception Handling
  • C++ Dynamic Memory
  • C++ Namespaces
  • C++ Templates
  • C++ Preprocessor
  • C++ Signal Handling
  • C++ Multithreading
  • C++ Web Programming