Google Interview Question
Software Engineer / DevelopersTeam: codeblock
Country: India
As per the question integer class has only one member function input..but u have used one more member function in public section..instead of using one more function in public block..why cant we make code the fn input() as...int input()
{ cout<<"enter integer"; cin>>inte;return inte}..and use this func in division as u used the output fn..we dont have condition that the protected memebr doesnt return int.do we?
#include<iostream>
using namespace std;
class integer
{
friend class character;
private:
const static int x=10;
};
class character
{
public:
char c;
void calc()
{
cout<<"Enter char: ";
cin>>c;
}
void disp()
{
integer a;
if(a.x%(c-48)==0)
cout<<"Divides Completely";
else
cout<<"
}
};
int main()
{
character b;
b.calc();
b.disp();
// system("pause");
return 0;
}
class Integer
{
int sb;
public:
Integer();
Integer(int a);
protected :
int Input(int);
};
class Character : public Integer
{
public:
Character();
Character(char ch);
void calc();
void display();
private:
char c;
};
int main()
{
Integer *i = new Integer();
int *p = (int *)i;
cout << *p << endl;
}
You *can*, but that doesn't mean you *should*. That's some seriously illegal hackery. It happens to work with usual C compilers but there's nothing that keeps the compiler from breaking it at any time, like by plugging in a garbage collecting allocator that makes the first field of any object an internal-use link item.
As it is not mentioned how many parameter I can take in function calc of class Character, I have used it to take two parameters.
#include <iostream>
using namespace std;
class Integer
{
protected:
void input(int data)
{
m_data = data;
}
private:
int m_data;
};
class Character : public Integer
{
public:
void calc(char inputChar, int inputInt)
{
Integer::input(inputInt);
if((inputChar <= 48) && (inputChar >=57))
{
cout<< "wrong input "<<endl;
return;
}
else
{
int temp = inputChar - 48;
if(temp%inputInt == 0)
{
display();
}
else
{
cout<< "Does not divide"<< endl;
}
}
}
void display()
{
cout<< "Divides Completely"<< endl;
}
private:
char m_char;
};
int main()
{
int tempInt;
char tempChar;
cout<< "Enter Integer - ";
cin>> tempInt;
cout<< "Enter Character - ";
cin>> tempInt;
Character tempCharObj;
tempCharObj.calc(tempChar, tempInt);
return 0;
}
#include <iostream>
using namespace std;
class Integer
{
protected:
void input(int data)
{
m_data = data;
}
private:
int m_data;
};
class Character : public Integer
{
public:
void calc(char inputChar, int inputInt)
{
Integer::input(inputInt);
if((inputChar <= 48) && (inputChar >=57))
{
cout<< "wrong input "<<endl;
return;
}
else
{
int temp = inputChar - 48;
if(temp%inputInt == 0)
{
display();
}
else
{
cout<< "Does not divide"<< endl;
}
}
}
void display()
{
cout<< "Divides Completely"<< endl;
}
private:
char m_char;
};
int main()
{
int tempInt;
char tempChar;
cout<< "Enter Integer - ";
cin>> tempInt;
cout<< "Enter Character - ";
cin>> tempInt;
Character tempCharObj;
tempCharObj.calc(tempChar, tempInt);
return 0;
}
#include <iostream>
using namespace std;
class Integer
{
protected:
void input(int data)
{
m_data = data;
}
private:
int m_data;
};
class Character : public Integer
{
public:
void calc(char inputChar, int inputInt)
{
Integer::input(inputInt);
if((inputChar <= 48) && (inputChar >=57))
{
cout<< "wrong input "<<endl;
return;
}
else
{
int temp = inputChar - 48;
if(temp%inputInt == 0)
{
display();
}
else
{
cout<< "Does not divide"<< endl;
}
}
}
void display()
{
cout<< "Divides Completely"<< endl;
}
private:
char m_char;
};
int main()
{
int tempInt;
char tempChar;
cout<< "Enter Integer - ";
cin>> tempInt;
cout<< "Enter Character - ";
cin>> tempInt;
Character tempCharObj;
tempCharObj.calc(tempChar, tempInt);
return 0;
}
#include <iostream>
using namespace std;
class Integer
{
public:
Integer()
{}
protected:
int input()
{
cout << "Enter integer: ";
cin >> data;
return data;
}
private:
int data;
}
class Character : public Integer
{
public:
Character()
{}
int calc()
{
int data = input();
cout << "Enter character: ";
cin >> sign;
return (data % (static_cast<int>(sign) - 48));
}
void display()
{
if(calc() == 0)
{
cout << "Divides Completely\n";
}
else
{
cout << "Does not divide\n"
}
}
private:
char sign;
}
- Joanna8848 October 29, 2012