You are on page 1of 12

4. What do you mean by C++ access specifiers ?

Access specifiers are used to define how the members (functions and
variables) can be accessed outside the class. There are three access
specifiers defined which are public, private, and protected
private:
Members declared as private are accessible only with in the same class and
they cannot be accessed outside the class they are declared.
public:
Members declared as public are accessible from any where.
protected:
Members declared as protected can not be accessed from outside the class
except a child class. This access specifier has significance in the context of
inheritance.
What are the differences between C and C++?
1) C++ is a kind of superset of C, most of C programs except few exceptions
(See this and this) work in C++ as well.
2) C is a procedural programming language, but C++ supports both
procedural and Object Oriented programming.
3) Since C++ supports object oriented programming, it supports features like
function overloading, templates, inheritance, virtual functions, friend
functions. These features are absent in C.
4) C++ supports exception handling at language level, in C exception
handling is done in traditional if-else style.
5) C++ supports references, C doesnt.
6) In C, scanf() and printf() are mainly used input/output. C++ mainly uses
streams to perform input and output operations. cin is standard input stream
and cout is standard output stream.
There are many more differences, above is a list of main differences.
What are the differences between references and pointers?
Both references and pointers can be used to change local variables of one

function inside another function. Both of them can also be used to save
copying of big objects when passed as arguments to functions or returned
from functions, to get efficiency gain.
Despite above similarities, there are following differences between references
and pointers.
References are less powerful than pointers
1) Once a reference is created, it cannot be later made to reference another
object; it cannot be reseated. This is often done with pointers.
2) References cannot be NULL. Pointers are often made NULL to indicate that
they are not pointing to any valid thing.
3) A reference must be initialized when declared. There is no such restriction
with pointers
Due to the above limitations, references in C++ cannot be used for
implementing data structures like Linked List, Tree, etc. In Java, references
dont have above restrictions, and can be used to implement all data
structures. References being more powerful in Java, is the main reason Java
doesnt need pointers.
References are safer and easier to use:
1) Safer: Since references must be initialized, wild references like wild
pointers are unlikely to exist. It is still possible to have references that dont
refer to a valid location (See questions 5 and 6 in the below exercise )
2) Easier to use: References dont need dereferencing operator to access the
value. They can be used like normal variables. & operator is needed only at
the time of declaration. Also, members of an object reference can be
accessed with dot operator (.), unlike pointers where arrow operator (->) is
needed to access members.
What are virtual functions Write an example?
Virtual functions are used with inheritance, they are called according to the
type of object pointed or referred, not according to the type of pointer or
reference. In other words, virtual functions are resolved late, at runtime.
Virtual keyword is used to make a function virtual.
Following things are necessary to write a C++ program with runtime
polymorphism (use of virtual functions)
1) A base class and a derived class.

2) A function with same name in base class and derived class.


3) A pointer or reference of base class type pointing or referring to an object
of derived class.
For example, in the following program bp is a pointer of type Base, but a call
to bp->show() calls show() function of Derived class, because bp points to an
object of Derived class.
#include<iostream>
using namespace std;

class Base {
public:
virtual void show() { cout<<" In Base \n"; }
};

class Derived: public Base {


public:
void show() { cout<<"In Derived \n"; }
};

int main(void) {
Base *bp = new Derived;
bp->show(); // RUN-TIME POLYMORPHISM
return 0;
}
Run on IDE
Output:
In Derived

What is this pointer?


The this pointer is passed as a hidden argument to all nonstatic member
function calls and is available as a local variable within the body of all
nonstatic functions. this pointer is a constant pointer that holds the memory
address of the current object. this pointer is not available in static member
functions as static member functions can be called without any object (with
class name).
Can we do delete this?
See http://www.geeksforgeeks.org/delete-this-in-c/
What are VTABLE and VPTR?
vtable is a table of function pointers. It is maintained per class.
vptr is a pointer to vtable. It is maintained per object (See this for an
example).
Compiler adds additional code at two places to maintain and use vtable and
vptr.
1) Code in every constructor. This code sets vptr of the object being created.
This code sets vptr to point to vtable of the class.
2) Code with polymorphic function call (e.g. bp->show() in above code).
Wherever a polymorphic call is made, compiler inserts code to first look for
vptr using base class pointer or reference (In the above example, since
pointed or referred object is of derived type, vptr of derived class is
accessed). Once vptr is fetched, vtable of derived class can be accessed.
Using vtable, address of derived derived class function show() is accessed
and called.
You may also like:
Practice Quizzes on C++
C/C++ Articles
We will soon be covering more C++. Please write comments if you find
anything incorrect, or you want to share more information about the topic
discussed above

How to test a number is palindrome or not in c++


A palindrome number is a number such that if we reverse it, it will not
change. For example some palindrome numbers examples are 121, 212,

12321, -454.
#include<iostream>

int checkPalindrome(int);
int main(){
int num,sum;

cout << "Enter a number: ";


cin >> num;

sum = checkPalindrome(num);

if(num==sum)
cout << num << " is a palindrome";
else
cout << num << " is not a palindrome";

return 0;

int checkPalindrome(int num){

static int sum=0,r;

if(num!=0){
r=num%10;
sum=sum*10+r;
checkPalindrome(num/10);
}

return sum;
}

Perfect number code in c++


a positive integer that is equal to the sum of its proper divisors. The smallest
perfect number is 6, which is the sum of 1, 2, and 3. Other perfect numbers
are 28, 496, and 8,128. The

#include<iostream>
#include<iomanip>
int main(){
int n,i=1,sum=0;

cout<<"Enter a number: ";


scanf("%d",&n);

while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
cout << i << " is a perfect number";
else
cout << i << " is not a perfect number";

return 0;
}
Swapping of two numbers without third variable
You can also swap two numbers without using temp or temporary or third
variable. In that case c program will be as shown :-

#include <stdio.h>

int main()
{
int a, b;

printf("Enter two integers to swap\n");


scanf("%d%d", &a, &b);

a = a + b;
b = a - b;
a = a - b;

printf("a = %d\nb = %d\n",a,b);


return 0;
}

Swap two numbers using pointers


#include <stdio.h>

int main()
{
int x, y, *a, *b, temp;

printf("Enter the value of x and y\n");


scanf("%d%d", &x, &y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

a = &x;
b = &y;

temp = *b;
*b = *a;

*a = temp;

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

Swapping numbers using call by reference


In this method we will make a function to swap numbers.

#include <stdio.h>

void swap(int*, int*);

int main()
{
int x, y;

printf("Enter the value of x and y\n");


scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}

void swap(int *a, int *b)


{
int temp;

temp = *b;
*b = *a;
*a = temp;
}

Pointer

Array

1. A pointer is a place in memory that keeps address of another place inside

1. An array is a single, pre allocated chunk of contiguous elements (all of the


same type), fixed in size and location.

2. Pointer cant be initialized at definition.

2. Array can be initialized at definition. Example

int num[] = { 2, 4, 5}

3. Pointer is dynamic in nature. The memory allocation can be resized or


freed later.

3. They are static in nature. Once memory is allocated , it cannot be resized


or freed dynamically.

4. The assembly code of Pointer is different than Array

4. The assembly code of Array is different than Pointer.

24

Answer

Explanation :
Looking at the diagram in rows, the central circle equals
half the sum of the numbers in the other circles to the left and right of the
centre.

Answer

Explanation :
Reading each pair of numbers as a 2 digit number, they
follow the sequence of square numbers from 6 to 9.

find the value of A??

AB
+AB
BCC
ABC=612,

AB

ABA

+CD
AAA
1596

AB

*A

AB

CBD5
5927

BCC
126

You might also like