No, friend functions do not follow access specifier rules as they are not members function. Friend function declared in any access specfier can access private data of the class using object.
#include<iostream>
using namespace std;
class sample
{
private:
int a;
private:
void privateNormalFunction(){};
private:
friend void f1(sample);
protected:
void friend f2(sample);
public:
friend void f3(sample);
};
void f1(sample s)
{
s.a=10;
cout<<"f1::a"<<s.a<<endl;
}
void f2(sample s)
{
s.a=10;
cout<<"f2::a"<<s.a<<endl;
}
void f3(sample s)
{
s.a=10;
cout<<"f3::a"<<s.a<<endl;
}
int main()
{
sample s;
f1(s);
f2(s);
f3(s);
return 0;
}
Private virtual function
//Can virtual function be private?
//Suppose virtual function vf() is declared as private in base.Even though vf is overriden as public in derived compiler will throw error of private at run time. But one work around could be given to declare a virtual function as private by making main() as friend function of that class.Because friend functions can access the private members.
#include<iostream>
using namespace std;
class base
{
private:
virtual void vf(){cout<<"base::vf"<<endl;}
//friend int main();
};
class derived:public base
{
public:
void vf(){cout<<"derved::vf;"<<endl;}
};
int main()
{
base* bp = new derived;
bp->vf();//error: ‘virtual void base::vf()’ is private
return 0;
}
//Suppose virtual function vf() is declared as private in base.Even though vf is overriden as public in derived compiler will throw error of private at run time. But one work around could be given to declare a virtual function as private by making main() as friend function of that class.Because friend functions can access the private members.
#include<iostream>
using namespace std;
class base
{
private:
virtual void vf(){cout<<"base::vf"<<endl;}
//friend int main();
};
class derived:public base
{
public:
void vf(){cout<<"derved::vf;"<<endl;}
};
int main()
{
base* bp = new derived;
bp->vf();//error: ‘virtual void base::vf()’ is private
return 0;
}
Vtable, vptr content and size postmortem
#include<iostream>
using namespace std;
class EmptyClass
{};
class PolymorphicVF
{
virtual void vf(){}
};
class PolymorphicDesstructor
{
virtual ~PolymorphicDesstructor()
{
}
};
class PolymorphicPureDestructor
{
virtual ~PolymorphicPureDestructor()=0;
};
class AbstractClassWithOnePureVF
{
virtual void pvf()=0;
};
class classWithOnepureVFAnd4vf
{
virtual void vf1(){};
virtual void vf2(){};
virtual void pvf()=0;
virtual void vf3(){};
virtual void vf4(){};
};
class Base
{
public:
virtual void vf(){}
};
class SingleDerivedNoOverridevf: public Base
{
};
class SingleDerivedNoOverridevf1: public Base
{
};
class SingleDerivedNoOverridevf2AndOneDM: public Base
{
public:
char c;
};
class SingleDerivedWithOverridevf: virtual public Base
{
public:
void vf(){}
};
class SingleDerivedWithOverridevf1: virtual public Base
{
public:
void vf(){}
};;
class DiamiondProblemWithNoFinalOverrider:public SingleDerivedNoOverridevf, public SingleDerivedNoOverridevf1
{
};
class DiamiondProblemWithFinalOverrider:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1
{
void vf(){}
};
class DiamondDerivedWithThree:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1, public SingleDerivedNoOverridevf1
{
void vf(){}
};
class DerivedWithThreeDifferent:public EmptyClass,public PolymorphicVF,public PolymorphicDesstructor
{};
int main()
{
cout<<"sizeof(EmptyClass)="<<sizeof(EmptyClass)<<endl;//1
cout<<"sizeof(PolymorphicVF)="<<sizeof(PolymorphicVF)<<endl;//8
cout<<"sizeof(PolymorphicDesstructor)="<<sizeof(PolymorphicDesstructor)<<endl;//8
cout<<"sizeof(PolymorphicPureDestructor)="<<sizeof(PolymorphicPureDestructor)<<endl;//8
cout<<"sizeof(AbstractClassWithOnePureVF)="<<sizeof(AbstractClassWithOnePureVF)<<endl;//8
cout<<"sizeof(classWithOnepureVFAnd4vf)="<<sizeof(classWithOnepureVFAnd4vf)<<endl;//8
cout<<"sizeof(Base with one vf)="<<sizeof(Base)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf)="<<sizeof(SingleDerivedNoOverridevf)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf2AndOneDM)="<<sizeof(SingleDerivedNoOverridevf2AndOneDM)<<endl;//16
cout<<"sizeof(SingleDerivedWithOverridevf)="<<sizeof(SingleDerivedWithOverridevf)<<endl;//8
cout<<"sizeof(DiamiondProblemWithNoFinalOverrider)="<<sizeof(DiamiondProblemWithNoFinalOverrider)<<endl;//16
cout<<"sizeof(DiamiondProblemWithFinalOverrider)="<<sizeof(DiamiondProblemWithFinalOverrider)<<endl;//16
cout<<"sizeof(DiamondDerivedWithThree)="<<sizeof(DiamondDerivedWithThree)<<endl;//24
cout<<"sizeof(DerivedWithThreeDifferent)="<<sizeof(DerivedWithThreeDifferent)<<endl;//24
return 0;
}
/*
sizeof(EmptyClass)=1
sizeof(PolymorphicVF)=8
sizeof(PolymorphicDesstructor)=8
sizeof(PolymorphicPureDestructor)=8
sizeof(AbstractClassWithOnePureVF)=8
sizeof(classWithOnepureVFAnd4vf)=8
sizeof(Base with one vf)=8
sizeof(SingleDerivedNoOverridevf)=8
sizeof(SingleDerivedNoOverridevf2AndOneDM)=16
sizeof(SingleDerivedWithOverridevf)=8
sizeof(DiamiondProblemWithNoFinalOverrider)=16
sizeof(DiamiondProblemWithFinalOverrider)=16
sizeof(DiamondDerivedWithThree)=24
sizeof(DerivedWithThreeDifferent)=16
*/
//How to check content of vtable
//compiled with command:
//g++ -fdump-class-hierarchy vtable.cpp
//vtables are taken from generated class table file by above command:
//vtable.cpp.002t.class
Class EmptyClass
size=1 align=1
base size=0 base align=1
EmptyClass (0x0x7fc113139d80) 0 empty
//No vtable for non polymorphic class EmptyClass
Vtable for PolymorphicVF
PolymorphicVF::_ZTV13PolymorphicVF: 3u entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI13PolymorphicVF)
16 (int (*)(...))PolymorphicVF::vf
Class PolymorphicVF
size=8 align=8
base size=8 base align=8
PolymorphicVF (0x0x7fc113139de0) 0 nearly-empty
vptr=((& PolymorphicVF::_ZTV13PolymorphicVF) + 16u)
using namespace std;
class EmptyClass
{};
class PolymorphicVF
{
virtual void vf(){}
};
class PolymorphicDesstructor
{
virtual ~PolymorphicDesstructor()
{
}
};
class PolymorphicPureDestructor
{
virtual ~PolymorphicPureDestructor()=0;
};
class AbstractClassWithOnePureVF
{
virtual void pvf()=0;
};
class classWithOnepureVFAnd4vf
{
virtual void vf1(){};
virtual void vf2(){};
virtual void pvf()=0;
virtual void vf3(){};
virtual void vf4(){};
};
class Base
{
public:
virtual void vf(){}
};
class SingleDerivedNoOverridevf: public Base
{
};
class SingleDerivedNoOverridevf1: public Base
{
};
class SingleDerivedNoOverridevf2AndOneDM: public Base
{
public:
char c;
};
class SingleDerivedWithOverridevf: virtual public Base
{
public:
void vf(){}
};
class SingleDerivedWithOverridevf1: virtual public Base
{
public:
void vf(){}
};;
class DiamiondProblemWithNoFinalOverrider:public SingleDerivedNoOverridevf, public SingleDerivedNoOverridevf1
{
};
class DiamiondProblemWithFinalOverrider:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1
{
void vf(){}
};
class DiamondDerivedWithThree:public SingleDerivedWithOverridevf, public SingleDerivedWithOverridevf1, public SingleDerivedNoOverridevf1
{
void vf(){}
};
class DerivedWithThreeDifferent:public EmptyClass,public PolymorphicVF,public PolymorphicDesstructor
{};
int main()
{
cout<<"sizeof(EmptyClass)="<<sizeof(EmptyClass)<<endl;//1
cout<<"sizeof(PolymorphicVF)="<<sizeof(PolymorphicVF)<<endl;//8
cout<<"sizeof(PolymorphicDesstructor)="<<sizeof(PolymorphicDesstructor)<<endl;//8
cout<<"sizeof(PolymorphicPureDestructor)="<<sizeof(PolymorphicPureDestructor)<<endl;//8
cout<<"sizeof(AbstractClassWithOnePureVF)="<<sizeof(AbstractClassWithOnePureVF)<<endl;//8
cout<<"sizeof(classWithOnepureVFAnd4vf)="<<sizeof(classWithOnepureVFAnd4vf)<<endl;//8
cout<<"sizeof(Base with one vf)="<<sizeof(Base)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf)="<<sizeof(SingleDerivedNoOverridevf)<<endl;//8
cout<<"sizeof(SingleDerivedNoOverridevf2AndOneDM)="<<sizeof(SingleDerivedNoOverridevf2AndOneDM)<<endl;//16
cout<<"sizeof(SingleDerivedWithOverridevf)="<<sizeof(SingleDerivedWithOverridevf)<<endl;//8
cout<<"sizeof(DiamiondProblemWithNoFinalOverrider)="<<sizeof(DiamiondProblemWithNoFinalOverrider)<<endl;//16
cout<<"sizeof(DiamiondProblemWithFinalOverrider)="<<sizeof(DiamiondProblemWithFinalOverrider)<<endl;//16
cout<<"sizeof(DiamondDerivedWithThree)="<<sizeof(DiamondDerivedWithThree)<<endl;//24
cout<<"sizeof(DerivedWithThreeDifferent)="<<sizeof(DerivedWithThreeDifferent)<<endl;//24
return 0;
}
/*
sizeof(EmptyClass)=1
sizeof(PolymorphicVF)=8
sizeof(PolymorphicDesstructor)=8
sizeof(PolymorphicPureDestructor)=8
sizeof(AbstractClassWithOnePureVF)=8
sizeof(classWithOnepureVFAnd4vf)=8
sizeof(Base with one vf)=8
sizeof(SingleDerivedNoOverridevf)=8
sizeof(SingleDerivedNoOverridevf2AndOneDM)=16
sizeof(SingleDerivedWithOverridevf)=8
sizeof(DiamiondProblemWithNoFinalOverrider)=16
sizeof(DiamiondProblemWithFinalOverrider)=16
sizeof(DiamondDerivedWithThree)=24
sizeof(DerivedWithThreeDifferent)=16
*/
//How to check content of vtable
//compiled with command:
//g++ -fdump-class-hierarchy vtable.cpp
//vtables are taken from generated class table file by above command:
//vtable.cpp.002t.class
Class EmptyClass
size=1 align=1
base size=0 base align=1
EmptyClass (0x0x7fc113139d80) 0 empty
//No vtable for non polymorphic class EmptyClass
Vtable for PolymorphicVF
PolymorphicVF::_ZTV13PolymorphicVF: 3u entries
0 (int (*)(...))0
8 (int (*)(...))(& _ZTI13PolymorphicVF)
16 (int (*)(...))PolymorphicVF::vf
Class PolymorphicVF
size=8 align=8
base size=8 base align=8
PolymorphicVF (0x0x7fc113139de0) 0 nearly-empty
vptr=((& PolymorphicVF::_ZTV13PolymorphicVF) + 16u)
Disadvantages of references in Cpp
References can not be re initialized. They always refer to first referent.
Reference arithmetic is not allowed. Operation effects on actual referent.
#include<iostream>
using namespace std;
int main()
{
int i=10;
int& j = i;
int & k =j;
cout<<"i: "<<i<<endl;
cout<<"j: "<<j<<endl;
cout<<"k: "<<k<<endl;
cout<<"&i: "<<&i<<endl;
cout<<"&j: "<<&j<<endl;
cout<<"&k: "<<&k<<endl;
//reference once initialized can not be reinitialed. point to first referent only.
int m=10;
int n=20;
int &x=m;
cout<<"x="<<x<<endl;//10
x=n;//this is not reassignment. actually its modifying value of m using x. this can be verified taking address of m and x as both would be same.
cout<<"x="<<x<<endl;//20
cout<<"m="<<m<<endl;//m gets modified to 20 now
cout<<"&x="<<&x<<endl;//0x7fff9671caf0
cout<<"&m="<<&m<<endl;//0x7fff9671caf0
//reference arithmatic is not allowed
int a=10;
int b=20;
int& ar=a;
cout<<"a="<<a<<endl;//10
cout<<"ar="<<ar<<endl;//10
ar++;//referant gets incremented not reference
cout<<"a="<<a<<endl;//11
cout<<"ar="<<ar<<endl;//11
return 0;
}
/*
i: 10
j: 10
k: 10
&i: 0x7fffee78603c
&j: 0x7fffee78603c
&k: 0x7fffee78603c
x=10
x=20
m=20
&x=0x7fffee786040
&m=0x7fffee786040
a=10
ar=10
a=11
ar=11
*/
Reference arithmetic is not allowed. Operation effects on actual referent.
#include<iostream>
using namespace std;
int main()
{
int i=10;
int& j = i;
int & k =j;
cout<<"i: "<<i<<endl;
cout<<"j: "<<j<<endl;
cout<<"k: "<<k<<endl;
cout<<"&i: "<<&i<<endl;
cout<<"&j: "<<&j<<endl;
cout<<"&k: "<<&k<<endl;
//reference once initialized can not be reinitialed. point to first referent only.
int m=10;
int n=20;
int &x=m;
cout<<"x="<<x<<endl;//10
x=n;//this is not reassignment. actually its modifying value of m using x. this can be verified taking address of m and x as both would be same.
cout<<"x="<<x<<endl;//20
cout<<"m="<<m<<endl;//m gets modified to 20 now
cout<<"&x="<<&x<<endl;//0x7fff9671caf0
cout<<"&m="<<&m<<endl;//0x7fff9671caf0
//reference arithmatic is not allowed
int a=10;
int b=20;
int& ar=a;
cout<<"a="<<a<<endl;//10
cout<<"ar="<<ar<<endl;//10
ar++;//referant gets incremented not reference
cout<<"a="<<a<<endl;//11
cout<<"ar="<<ar<<endl;//11
return 0;
}
/*
i: 10
j: 10
k: 10
&i: 0x7fffee78603c
&j: 0x7fffee78603c
&k: 0x7fffee78603c
x=10
x=20
m=20
&x=0x7fffee786040
&m=0x7fffee786040
a=10
ar=10
a=11
ar=11
*/
C program to find and replace a string in a sentence
C Program for finding a pattern in a sentence and replace it with another string.
//main string: Manoj Kumar Sahu
//string to be found and replaced: uma
//new string to be replaced with uma: UMA
//resultant string: Manoj KUMAr Sahu
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char* fr(char* str, char* old, char* new)
{
int count=0, i=0;
char* ret;
int lenold = strlen(old);
int lennew = strlen(new);
for(i=0;str[i]!='\0';i++)
{
if(strstr(&str[i],old)==&str[i])
{
count=count+1;
lenold=lenold-1;
}
else{}
}
ret=(char*)malloc(i+count*(lennew-lenold));
i=0;
while(*str)
{
//if(strstr(&str[i],old)=&str[i])//l value required
if(strstr(str,old)==str)
{
//copy
strcpy(&ret[i],new);
i=i+lennew;
str=str+lenold;
}
else
{
ret[i]=*str;
i++;
str++;
}
}
return ret;
}
int main()
{
char str[]="manoj kumar sahu";
char old[]="uma";
char new[]="UMA";
char *result=fr(str,old,new);
printf("resltanc string=%s\n",result);
return 0;
}
//main string: Manoj Kumar Sahu
//string to be found and replaced: uma
//new string to be replaced with uma: UMA
//resultant string: Manoj KUMAr Sahu
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char* fr(char* str, char* old, char* new)
{
int count=0, i=0;
char* ret;
int lenold = strlen(old);
int lennew = strlen(new);
for(i=0;str[i]!='\0';i++)
{
if(strstr(&str[i],old)==&str[i])
{
count=count+1;
lenold=lenold-1;
}
else{}
}
ret=(char*)malloc(i+count*(lennew-lenold));
i=0;
while(*str)
{
//if(strstr(&str[i],old)=&str[i])//l value required
if(strstr(str,old)==str)
{
//copy
strcpy(&ret[i],new);
i=i+lennew;
str=str+lenold;
}
else
{
ret[i]=*str;
i++;
str++;
}
}
return ret;
}
int main()
{
char str[]="manoj kumar sahu";
char old[]="uma";
char new[]="UMA";
char *result=fr(str,old,new);
printf("resltanc string=%s\n",result);
return 0;
}
Multiple Inheritance aptitude questions.Diamond problem
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//A::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//A::f
d.A::f();//A::f
d.B::f();//A::f
d.C::f();//A::f
d.D::f();//A::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and C::f and B::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f C::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();//error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are B::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//B::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
d.D::f();// B::f
cout<<""<<endl;
return 0;
}
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//A::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//A::f
d.A::f();//A::f
d.B::f();//A::f
d.C::f();//A::f
d.D::f();//A::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
public:
void f(){cout<<"D::f"<<endl;}
};
int main()
{
D d;
d.f();//D::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//C::f
d.D::f();//D::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are A::f and C::f and B::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//C::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f C::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
public:
void f(){cout<<"C::f"<<endl;}
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();//error: request for member ‘f’ is ambiguous
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
//d.f();//error: request for member ‘f’ is ambiguous//candidates are B::f and A::f
//d.A::f();//error: ‘A’ is an ambiguous base of ‘D’
d.B::f();//B::f
d.C::f();//A::f
//d.D::f();// error: request for member ‘f’ is ambiguous//candidates are A::f and B::f
cout<<""<<endl;
return 0;
}
//--
#include<iostream>
using namespace std;
class A
{
public:
void f(){cout<<"A::f"<<endl;}
};
class B:virtual public A
{
public:
void f(){cout<<"B::f"<<endl;}
};
class C:virtual public A
{
};
class D:public B, public C
{
};
int main()
{
D d;
d.f();//B::f
d.A::f();//A::f
d.B::f();//B::f
d.C::f();//A::f
d.D::f();// B::f
cout<<""<<endl;
return 0;
}
Remove consecutive repeated characters from a string.
//C program to remove consecutive repeated characters from string.
//C program to remove consecutive repeated elements from an array.
#include <stdio.h>
#include<string.h>
int main()
{
int i,j,len,len1;
char str[100];
printf("Enter a string with repeated adjacent characters:");
gets(str);
len=strlen(str);
//assign 0 to len1 - length of removed characters
len1=0;
//Removing consecutive repeated characters from string
for(i=0; i<(len-len1);)
{
if(str[i]==str[i+1])
{
//shift all characters
for(j=i;j<(len-len1);j++)
str[j]=str[j+1];
len1++;
}
else
{
i++;
}
}
printf("String after removing duplicate adjacent elements : %s\n",str);
return 0;
}
/*
Enter a string with repeated adjacent characters:abbcccdefabc
String after removing duplicate adjacent elements : abcdefabc
*/
//Removing consecutive members in an array
#include<iostream>
using namespace std;
class Base
{
};
void print(int* p)
{
while(*p)
cout<<*p++<<" ";
cout<<endl;
};
int main()
{
cout<<""<<endl;
int arr[]={1,2,2,3,3,2,4,5,5,55,555,5};
print(arr);
int size=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;)
{
if(arr[j]==arr[j+1])
{
for(int k=j;k<size;k++)
{
arr[k]=arr[k+1];
}
//shift
size--;
}
else
{
//check next
j++;
}
}
}
print(arr);
return 0;
}
/*
1 2 2 3 3 2 4 5 5 55 555 5
1 2 3 2 4 5 55 555 5
*/
//C program to remove consecutive repeated elements from an array.
#include <stdio.h>
#include<string.h>
int main()
{
int i,j,len,len1;
char str[100];
printf("Enter a string with repeated adjacent characters:");
gets(str);
len=strlen(str);
//assign 0 to len1 - length of removed characters
len1=0;
//Removing consecutive repeated characters from string
for(i=0; i<(len-len1);)
{
if(str[i]==str[i+1])
{
//shift all characters
for(j=i;j<(len-len1);j++)
str[j]=str[j+1];
len1++;
}
else
{
i++;
}
}
printf("String after removing duplicate adjacent elements : %s\n",str);
return 0;
}
/*
Enter a string with repeated adjacent characters:abbcccdefabc
String after removing duplicate adjacent elements : abcdefabc
*/
//Removing consecutive members in an array
#include<iostream>
using namespace std;
class Base
{
};
void print(int* p)
{
while(*p)
cout<<*p++<<" ";
cout<<endl;
};
int main()
{
cout<<""<<endl;
int arr[]={1,2,2,3,3,2,4,5,5,55,555,5};
print(arr);
int size=sizeof(arr)/sizeof(arr[0]);
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;)
{
if(arr[j]==arr[j+1])
{
for(int k=j;k<size;k++)
{
arr[k]=arr[k+1];
}
//shift
size--;
}
else
{
//check next
j++;
}
}
}
print(arr);
return 0;
}
/*
1 2 2 3 3 2 4 5 5 55 555 5
1 2 3 2 4 5 55 555 5
*/
Subscribe to:
Posts (Atom)