Friday, August 20, 2010

c++ notes 2

Protected member function can not access into main function but it can be access into the derived class.


What is virtual base class.

When we write two derived class derive from the base class than we write new derive class which also derive two above class. It is called the virtual base class.

Code:
Class Base
{
Public:
Int data;
Base()
{
Data =0;
}
};

Class derived : virtual public Base
{
};
Class derived1 : virtual public Base
{
};

Class derived3 : public derived , public derived
{
Public:
Int getdata()
{
Return data;
}
};
Int main()
{
Derived d3;
Int a =d3.getdata();
}

a)Some changes if we remove virtual keyword from the derived class.
Than it will be compiler error.

base.cpp: In method `int derived3::getdata()':
base.cpp:37: request for member `data' is ambiguous in multiple inheritance lattice
base.cpp:8: candidates are: int base::data
base.cpp:8: int base::data

http://stackoverflow.com/questions/1321062/object-layout-in-case-of-virtual-functions-and-multiple-inheritance

http://www.phpcompiler.org/articles/virtualinheritance.html

http://www.ziddu.com/download/16618915/jangrayhood.pdf.html


b) if we write public virtual Base rather than virtual public Base than it does not impact anything Program run successfully.

Virtual  keyword 


Class base
{
Public:
void fun()
{
Cout<<” base::fun()”<<endl;
}
};

Class derived : public Base
{
Fun()
{
Cout<<” derived ::fun()”<<endl;
}
}

Int main()
{
Base *p,base;
Derived d;
P=&base;
p->fun();
p=&d;
p->fun();
}


Output :

Base ::fun()
Base ::fun()

if virtual keyword than
 
Output:

Base ::fun()
Derived ::fun()

Third Case

Class Base
{
Public:
Void virtual fun() - There is no error if u write virtual void or void virtual
{
Cout<<” base :: fun”<<endl;
}

};
Class derived :public base
{
Public :
Void fun()
{
Cout<<” derived::fun”<<endl;
}
};
Int main()
{
Base *p,base;
Derived d;
P=&base;
p->fun();
p=&d;
p->fun();
return 0;
}


Output
Base ::fun()
Derived ::fun()


Fourth Case


Class Base
{
Public:
Virtual void fun()
{
Cout<<” Base:: fun()”<<endl;
}
};
Class derived : public base
{
Public:
fun()  Change the signature from the base class
{
Cout<<” Derived :: fun()”<<endl;
}

Int main()
{

Base *b,base;
Derived d;
b=&base;
b->fun();
b=&d;
b->fun();
}


Output:

virtual.cpp:22: ANSI C++ forbids declaration `fun' with no type
virtual.cpp:22: conflicting return type specified for virtual function `int derived::fun()'
virtual.cpp:8: overriding definition as `void base::fun()'

Fifth Case:

Class Base

{
Public :
Virtual void fun()
{
Cout<<”Base::fun()”<<endl;
}
};
Class derived : public Base
{
Public :
I have removed fun function from the derived class
}
Int main
{
Base *p,base;
Derived d;
p=&base;
p->fun();
p=&d;
p->fun();
}

Output

Base ::fun()
Base ::fun()

Case Sixth

Class Base
{
Public:
Void fun()
{
Cout<<” Base::fun()”<<endl;
}
};
Class derived : public Base
{
Public:
Virtual void fun()  Adding virtual keyword in the derived class
{
Cout<<” Derived::fun()”<<endl;
}
};
Int main()
{
Base *p,base;
Derived d;
P=&base;
p->fun();
p=&base;
p->fun();
}

output
Base ::fun()
Base ::fun()

Case Seven

class base{
public :
virtual void fun()
{
   cout<<" Base ::fun()"<<endl;

}
};


class derived : public base
{
   public:
   void fun()
   {
           cout<<" derived::fun()"<<endl;
    }


   void method()
   {
           cout<<" base::method::fun()"<<endl;
    }


}


int main()
{
    base *p;
    derived d;
    p=&d;
    p->method();
 }

Output: 


virtual.cpp: In function 'int main()':
virtual.cpp: error: 'class base' has no member named 'method'


Case Eight C) wrong syntax




We can write base class in following way
a) Correct Syntax

Int main()
{
Base *p,base;
Derived d;
P=&base

p->fun();
p=&d;
p->fun();
}


b) Correct Syntax
int main()
{
Base *p,base;
Derived d;
p=new derived;
p->fun()
}



Int main()
{
Derived *d,d1;
Base base;
d =new base
d->fun();
d=&d1;
d->fun();
}

Error: type `derived' is not a base type for type `base'

d) wrong syntax
int main()
{
Derived *d,d1;
Base base;
D=&base;
d->fun();
d=&d1;
d->fun();
}
Error : type `derived' is not a base type for type `base'

What is virtual function

A virtual function or virtual method is a function or method whose behavior
Can be overridden within an inheriting class by a function with the same signature

What is kind of and has a relationship
What is iterator and iteration

Iterator is an object which moves in the container from the one element to another element and access the variable. The process of moving one element

From another element called iteration
When should the explicit keyword be used in the constructor ?

When we want that constructor should create object but it should not perform any
Implicit conversion than we should use the explicit keyword.


#include<iostream>
Class sample
{
Int I;
Explicit Sample(int ii =0)
{
I=ii;
}

Sample operator+(sample s)
{
Sample temp;
temp.i =i+s.i;
return temp;
}
Void display()
{
Cout<<I;
}
}
Int main()
{
Sample s1(15),s2;
S2=s1+15; //compilation error
S2.display();
}

How will you change the data member of the const object.

To change the date member of the const object the data members are declared
As mutable in the class.

#include<iostream>
Class sample
{
Private:
Mutable int I; - Mutable keyword
Public:
Sample(int ii=0)
{
I=ii;
}
Void fun() const  const object can access const function
{
I++;
}
};
Int main()
{
Const sample s(15);  const object.
s.fun();
}

More info about Const Variable.
http://www.cprogramming.com/tutorial/const_correctness.html

http://www.parashift.com/c++-faq-lite/const-correctness.html 

http://duramecho.com/ComputerInformation/WhyHowCppConst.html

What is friend function and when should be a function be made friend

A global function that can access that all data member of a class is called a friend function of that class. To make any function a friend of class it must be declared with the keyword friend function.

A function is made friend in the following two cases.
To access private data of a class from a non member function

To increase the versatility of overloaded operator
How does a friend function help in increasing the versatility of the overloaded operators

S2=s1*2
Where s1 and s2 are objects of the sample class. The statement works if the overloaded operator * (sample s) or conversion function is provided in the class.

Internally this statement occurs
S2=s1.operator*(2);

The function materializes because it is called with an object S1 the this pointer
Of s1 implicitly get passed.

But if we want to write the above statement as
S2=2*s1;

#include<iostream>
Class sample
{
Private
Int I;
Public:
Sample(int ii =0)
{
I =ii;
}
Void showdata()
{
Cout<<i<<endl;
}
Friend sample operator*(sample,sample);
};
Sample operator*(sample s1,sample s2)
{
Sample temp;
Temp.i=s1.i +s2.i;
Retrun temp;
}
Int main()
{
Sample s1(10),s2;
S2=s1*2;
S2.showdata();
S1=s2*2;
S1.showdata();
}

What is forward referencing and when should be used?

Class sample
{
Public:
Friend void fun(sample,test);
};
Class test
{
Public:
Friend void fun(sample,test);
}

Int main()
{
Sample s;
Test t; }

Write one case where friend function becomes necessary

When we want user user defined object should be print through cout than we overload operator <<.

Class sample
{
Char name[20];
Int age;
Float sal;

Public:
Friend ostream& operator<<(ostream &o, sample &a);
Friend istream& operator>>(istream &I,sample &a);
}
Ostream& operator<<(ostream &o, sample &a)
{
O<<a.name<<a.age<<a.sal;
Return o;
}
Istream& operator>>(istream &I, sample &a)
{
i>>a.name>>a.age>>a.sal;
return I;
}
Int main()
{
Sample s;
Cin>>s;
Cout<<s;
}

Is it necessary that while using pointer to member with data the data must be made public

Yes the private data member are never accessible outside the class

How would you define a point to data member of the type pointer to pointer

Class sample
{
Public:
Sample(int **pp)
{
p=pp;
}
Int **p;
}
Int **sample:: *ptr = &sample::p;
Int main()
{
Int i=9;
Int *s =&I;
Sample sa(&s);
Cout<<**(s.*ptr);
}

Can we create a pointer to member function
Class sample
{
Public:
Void fun()
{
Cout<<”func with the pointer”<<endl;
}
};
Void (sample:: *pfun()) =&sample::fun;
Int main()
{
Sample s;
(s.*pfub()) ();
}

Some more info
http://www.eskimo.com/~scs/cclass/int/sx10b.html

What is namespace

While developing a program different header file are included. It may happen that some header file contain same function or class name. Trying to access these function or classed may lead to be an ambiguity. Such ambiguity can be

Avoided using namespace.
How to refer to a name of class or function that is defined within a namespace

Namespace name1
{
Class sample1
{
//code
}
}
Namespace name2
{
Class sample2
{
//code
}
}
Using namespace sample2;
Int main()
{
Name1:: sample s1;
Sample2 s2;
}


What is RTTI

RTTI stands for run time type identification. In the inheritance hierarchy using RTTI we can find exact type of object using a pointer or reference to the base class. The idea behind that virtual function is to upcast the derived class objectAddress into a pointer to a base class object and then the virtual function

Mechanism implement the correct behavior.

No comments:

Post a Comment