#include <iostream>
using namespace std;
int main()
{
int ____123 =123;
cout <<"Hello World! "<<____123<<"\n";
return 0;
}
A : Yes the above program is valid. in C & C++ identifier name can/should start with _ or [a-zA-Z]. It cant contain any space or special characters in it.Q : Write a program to add two numbers without using assignment operator.
A : Remember when we were kids we used to add numbers one by one counting the fingers. Example 5 + 4 = 5 + 1 = 6 + 1 = 7 + 1 = 8 + 1 = 9 . Simple problem solved :) Add one by one in a loop using ++ operator.
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter Two Numbers :";
cin >>a>>b;
while(b--)++a;
cout<<"Result :"<<a<<endl;
}
Q : in C++ can a have Anonymous class?
A : In C++ classes can be anonymous ie., they can be declared without an identifier.
#include<iostream>
using std::cout;
class{
private:
int a;
public:
void init()
{
a=10;
}
void display()
{
cout<<"i m display() - member function "<<a<<"\n";
}
}A,B;
int main()
{
A.init();
A.display();
B.display();
}
Output:
i m display() - member function 10
i m display() - member function 0
Here A and B are the instance of the anonymous class. Member functions of anonymous classes can be called like normal class member functions.
Anonymous classes comes with some restrictions.
Q : Inheritance and Anonymous class is it valid?
A : Yes. Check the below program
#include<iostream>
#include <stdarg.h>
using std::cout;
class Base
{
public:
int a;
Base():a(7){}
void parentDisplay(){
cout<<"Parent Class value of a :"<<a<<"\n";
}
};
class : public Base
{
public:
void childDisplay()
{
cout<<"Child Class - ";
parentDisplay();
}
} AnonymousInherited;
int main()
{
AnonymousInherited.parentDisplay();
AnonymousInherited.childDisplay();
}
Output:
Parent Class value of a :7
Child Class - Parent Class value of a :7
Q : Tell something about anonymous union?
A : Union can be anonymous. But we should know the restrictions of anonymous unions. They are
Example:
#include<iostream>
using std::cout;
int main()
{
union Base
{
int x;
};
union : Base // Error here
{
int a;
char c;
void display(){cout<<" a : "<<a<<"\n";} // Error here
};
a = 97;
cout<<" c : "<<c<<"\n";
}
I owe you a world full of thanks for the post. It was really nice and amazing post.
ReplyDelete____________________
Employee Engagement Programs