standard C++ OOP features (codes)



#include "stdafx.h"



#include <cstdlib>



#include <iostream>





#define OUT





using namespace std;





class A



{



private:



int
iMyNum;



void
APrivateFunc()



{





}





public:



A()



{



iMyNum = 100;



}


friend ostream& operator<<( ostream&, A& ) ;
//made as friend function for performance reason



void
MyFunc()



{



printf("myfunct");



}



};

ostream&operator<<( ostream& o, A& theObj ){ o << "A" << endl; return o ;}



class B : private A



{







public:



B():A()



{



}



int iMyB;



using
A::MyFunc;



//only for the
member variables which already originaly



//accessible but
during inheris access specifier specify as private instead of public



//using
A::APrivateFunc;



//this is not
valid APrivateFunc before that is a private function.



};







class A1



{



protected:



int iMyA1;









public:



int
GetMyA1()



{return
iMyA1;}







};





class B1:public A1



{







public:



int iMyB1;





B1():A1()



{



}





B1 & operator
= (A1 &a1)



{



A1::iMyA1 = a1.GetMyA1();







return
*this;



}









};





// o (based)



// /\



// o o (derived)



// \ /



// o (grandchild)





//hybrid inheritance base class



class Base



{



int iMyBased;





public:



Base()



{}



Base(int
i)



{iMyBased = i;}



void
MyBased(){



cout << "MyBased" ;



}



};





//virtual base class to solve hybrid inheritance problem when calling
common based function



class DerivedA :
public virtual Base



{





public :





void
DerivedFunc()



{



}





DerivedA()



{



}





DerivedA(int
i):Base(i)



{





}



};





class DerivedB :
public virtual Base



{





public :





void
DerivedFunc()



{



}







DerivedB()



{



}





DerivedB(int
i):Base(i)



{





}



};





class
GrandChild : public
DerivedA, public DerivedB



{



public:





GrandChild():DerivedB(), DerivedA()



{}



//GrandChild(int
i):DerivedB(i), DerivedA(i) //the problem is that if the base class is virtual,
only the default base class constructor will be called.



//{} i.e
- the Base(int i) will not be called eventhough the constructor had instructed
to do so.



GrandChild(int
i):Base(i), DerivedB(i), DerivedA(i)
//to solve the
problem above



{}





void
MyCall()



{



Base::MyBased();



}



};





class String



{





private:



char
*Strng;





public:





void
GetData()



{



cout << "Type the data\n";



Strng = new char[15];



cin >> Strng;



}





void
ShowData()



{



cout << Strng << "\n";





}











String &operator = (String &Strn)



{



printf("operator assignment");



int
Len = strlen(Strn.Strng);



Strng = new char[Len + 1];



strcpy(Strng,
Strn.Strng);



return
*this;



}





String(){}





String (const
String &Strn)



{



printf("copy
const"
);



int Len = strlen(Strn.Strng);



Strng = new char[Len + 1];



strcpy(Strng,
Strn.Strng);



}









explicit String(char
*psz)



{



printf("explicit cast");



int
Len = strlen(psz);



Strng = new char[Len + 1];



strcpy(Strng, psz);



}





operator char*()



{




printf("operator typename");




return Strng;



}





~String()



{



delete
[] Strng;



}





};









class Host



{



private:



int iMyID;





public:



Host()



{}



Host(const
Host&){}



Host & operator
= (Host &h){}



friend class Parasite; //establish
friend relationship



};





class Parasite



{



private:



int iMyID;





public:



friend void accessParasite(OUT Parasite &p, int ID); //this allow non
member or member of class to be able to access private data members.



Parasite(){}



void
SetHostID(OUT Host &h, int ID){h.iMyID =
ID;}
//this become possible if Parasite become Host
friend class.



};





void
accessParasite(OUT Parasite &p, int ID)



{



p.iMyID = ID;



}









class v1



{



public:



/*v1(){}



~v1(){}*/



//when a class
definition contain a pure virtual that definition undefined, that mean it is a
abstract class,



//meaning it can
not be instantiated. If inheritted class do not define the definition of that
parent virtual



//function, the
inheritted class also become abstract.



//pure virtual
save from having to write definition into function.



//__declspec(novtable)
for VC++ can be use as it save storage for each of the inheritted object



virtual char* myPVirtual() const
=0;
//the const mean the function can not modify the
member variable



virtual void myV1(){cout << "v1-myV1"<<
endl;}



void
myNonVirtual(){cout << "v1-myNonVirtual"
<< endl;}



};





class dv1: public v1



{



public:



dv1():v1(){}



~dv1(){}



virtual
char*

myPVirtual() const



{



printf("myMyVirtual");



return
NULL;



}



virtual
void myV1(){cout << "dv1-myV1"<< endl;}



void
myNonVirtual(){cout << "dv1-myNonVirtual"
<< endl;}





};







class dv2: public v1



{



public:



dv2():v1(){}



~dv2(){}



virtual
char*

myPVirtual() const



{



printf("myMyVirtual");



return
NULL;



}



void
myV1(){cout << "dv2-myV1"<<
endl;}



void
myNonVirtual(){cout << "dv2-myNonVirtual"
<< endl;}





};





class dv2d:public dv2



{



public:



void
myV1(){cout << "dv2d-myV1"<<
endl;}



};





int main(int argc, char
*argv[])



{



String str1;



str1.GetData();



str1.ShowData();



String str2(str1); //copy
constructor





String str3 = str2; //copy constructor



String str4;



str4 = str3; //operator
= called





str2.ShowData();



str2.GetData();



str1.ShowData();





String str5;





char
*pszTest = "hello";



str5 = String(pszTest); //explicit cast called







String str6;





str6 = (String)pszTest; //explicit cast called









char
*pCheck = (char*)str1;
//operator
typename called (it works like text sub internally.)



cout << pCheck;











A1 *a;





B1 b;



b.iMyB1 = 1000;





a = &b;





B1 *b2;



b2 = (B1*)a; //this
will still be able to get the retained value.



printf("%d",
b2->iMyB1);







A1 aa;





/*B1 b3;



b3 =
reinterpret_cast<B1>(*a);*/



B1 b3;



b3.iMyB1 = 123;



aa = b3; //from
derived to base, can







B1 b4;



b4 = aa; //from
base to derived, need conversion function for the B1 assignment operator or
copy construtor







GrandChild oo;



oo.MyCall();



oo.MyBased(); //this
will prompt error if not using virtual base class, in the code write
"virtual public classname" as inherit access modifier





oo.DerivedA::DerivedFunc(); //this will resolve ambiguity problem.





cout << endl << "----------------" << endl ;







v1 *v;



dv1 dv_1;



dv2 dv_2;







v = &dv_1;



v->myV1();



v->myNonVirtual(); //non virtual will not be able to make the function call to
be based on the



//the real class it points to. (meaning, polymophism needs
virtual function.)





v = &dv_2;



v->myV1();



v->myNonVirtual();





dv2d dv_2d;



dv2 *pDV2;



pDV2
=&dv_2d;



pDV2->myV1(); //eventhough
the dv2 does not declare the myV1 as virtual anymore but only its parent, but
the derived



//child
still be able to make the function call to be based on the



//the real class it
points to.









cout << "----------------"
<< endl;











system("PAUSE");



return
EXIT_SUCCESS;



}



//composite relationship



class
CompositeOrContained



{



public:



CompositeOrContained(){}



CompositeOrContained(int i){}



};





class
ContainerOrComposed



{



public:



CompositeOrContained m_o;





ContainerOrComposed(){}



ContainerOrComposed(int i):m_o(i)



{



}



};





//associative relationship



class
Associated;



class
Associating



{





public:



Associated *pA;







};





class Associated



{



public:



Associated(){}



~Associated(){}





};





//associative with custody relationship



class
AssociatingWithCustody



{





public:



Associated *pA;



AssociatingWithCustody(){pA = new Associated();}



~AssociatingWithCustody(){delete pA;}





};








Comments

Popular posts from this blog

Outlook : "operation failed, object could not be found. " when trying to close a PST.

How to transfer app and data from old iPhone to new iPhone for model IOS17 and IOS18 above.

Tips on how to use IBM iNotes (web client) on emailing or scheduling - Like copy tables from Excel or checking for user id (windows logon id) through email address