Oops
Oops
ANS:-
The OOP, can be considered to be a type of structured
programming which uses structured programming techniques
for program flow, but adds more structure for data to be
modelled. We have highlighted some of the basic differences
between the two as under:
1) OOP is closer to the phenomena in real world due to the
use of objects whereas structured programming is a bit away
from the natural way of thinking of human beings.
class abstracTon
{
private:
int x, y;
public:
int main()
{
abstracTon sp;
sp.set(20, 30);
sp.display();
return 0;
}
Output:
Sum = 95
In the program above, you can see that we are not allowed to
directly access the variables x and y. But you can call the set()
funcTon to set the values x and y. The display() funcTon is
called to display the values x and y.
Advantages of data abstracTon
• It prevents the user from wriTng low-level code.
• It prevents duplicaTon of sogware and increases
reusability.
• The internal class implementaTon can be altered
without impacTng the user.
• It helps to improve the privacy of an applicaTon or
program as the user is only presented with relevant
informaTon.
QUES:- Define friend FuncTon?
Ans:- A friend funcTon in C++ is defined as a funcTon that can
access private, protected and public members of a class. The
friend funcTon is declared using the friend keyword inside
the body of the class
• Even though the prototypes for friend funcTons appear
in the class definiTon, friends are not members funcTons.
• We can declare friend funcTons anywhere in a class
definiTon, that is either in public, private or protected
secTons.
• We can do friend declaraTons anywhere in a class
definiTon, i.e. either in public, private or protected secTons.
• Violates the data hiding principle of classes, so we
should avoid it as much as possible.
• You can grant friendship but not take it, i.e., for class B
to be a friend of class A, class A must explicitly declare that
class B is its friend.
• The friendship relaTon is neither symmetric nor
transiTve.
Friend relaTonship cannot be inherited.
Benefits of friend funcTon
• A friend funcTon is used to access the non-public
members of a class.
• It allows to generate more efficient code.
• It provides addiTonal funcTonality which is not normally
used by the class.
• It allows to share private class informaTon by a non
member funcTon
CharacterisTcs:-
• The funcTon is not in the scope of the class to which it
has been declared as a friend.
• It cannot be called using the object as it is not in the
scope of that class.
• It can be invoked like a normal funcTon without using
the object.
• It cannot access the member names directly and has to
use an object name and dot membership
• operator with the member name.
example
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. private:
6. int length;
7. public:
8. Box(): length(0) { }
9. friend int printLength(Box); //friend funcTon
10. };
11. int printLength(Box b)
12. {
13. b.length += 10;
14. return b.length;
15. }
16. int main()
17. {
18. Box b;
19. cout<<"Length of box: "<< printLength(b)<<endl;
20. return 0;
21. }
Output:
Length of box: 10
Ques:- Define staTc member?
Ans:- staTc members are data members (variables) or
methods that belong to a staTc or a non staTc class itself,
rather than to objects of the class. StaTc members always
remain the same, regardless of where and how they are used.
Because staTc members are associated with the class, it is
not necessary to create an instance of that class to invoke
them.
Since a staTc variable is associated with the class, only one
copy of the variable exists in memory. This copy is shared by
all objects of that class.
Some of the features of staTc members are as follows:
• A staTc member has access to all staTc members of its
containing class, including private members.
• A staTc member can be declared using access control
modifiers.
• A staTc member class can use any other staTc member
without qualifying its name with the name of the containing
class.
ProperTes of staTc member funcTons:
1. A staTc funcTon can only access other staTc variables or
funcTons present in the same class
2. StaTc member funcTons are called using the class name.
Syntax- class_name::funcTon_name( )
example
#include <iostream>
public:
void set_n(){
n = ++Number;
}
void show_n(){
cout<<"value of n = "<<n<<endl;
}
};
int main()
{
Example example1, example2;
example1.set_n();
example2.set_n();
example1.show_n();
example2.show_n();
Example::show_Number();
return 0;
}
Output:
From the above output, we can see that the value of the
variable ‘n’ is different for both the objects ‘example1’ and
‘example2’ of the class ‘Example’. Since the variable ‘Number’
is a class variable its value is the same for both the objects
‘example1’ and ‘example2’. StaTc member variables and
funcTons are used when common values are to be shared
across all the objects. While programming, the use of staTc
keyword should be done wisely.
# include<iostream.h>
# include<conio.h>
class student
{
public:
student() // user defined constructor
{
cout<< “object is iniTalized”<<end1;
}
};
void main ()
{
student x,y,z;
getch();
}
int main()
{
student s; // default construtor
s.show_data();
student A(s); // copy constructor
A.show_data();
getch();
return 0;
}
QUES: DEFINE CONSTRUCTOTR OVERLOADING?
ANS:-
Constructor Overloading When more than one constructor
funcTon is defined in a class, then it is called constructor
overloading or use of mulTple constructor in a class. It is used
to increase the flexibility of a class by having more number of
constructors for a single class. Overloading constructors in
C++ programming gives us more than one way to iniTalize
objects in a class.
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
int roll;
char name[30];
public:
student(int x, char y[]) // parameterized constructor
{
roll =x;
strcpy(name,y);
}
student() // normal constructor
{
roll =100;
strcpy(name,"y");
}
void input_data()
{
cout<<"\n Enter roll no :"; cin>>roll;
cout<<"\n Enter name :"; cin>>name;
}
void show_data()
{
cout<<"\n Roll no :"<<roll;
cout<<"\n Name :"<<name;
}
};
int main()
{
student s(10,"z");
s.show_data();
getch();
return 0;
}
#include <iostream>
using namespace std;
class DemoDC {
private:
int num1, num2 ;
public:
DemoDC() {
num1 = 10;
num2 = 20;
}
void display() {
cout<<"num1 = "<< num1 <<endl;
cout<<"num2 = "<< num2 <<endl;
}
};
int main() {
DemoDC obj;
obj.display();
return 0;
}
Output
num1 = 10
num2 = 20
class Person {
private:
int age;
public:
// 1. Constructor with no arguments
Person() {
age = 20;
}
int getAge() {
return age;
}
};
int main() {
Person person1, person2(45);
return 0;
}
Output
Person1 Age = 20
Person2 Age = 45
QUES:- EXPLAIN WITH EXAMOPLE CONTINUE,BREAK,EXIT
GOTO STMTS
ANS:-
In C++, the break statement terminates the loop when it is
encountered.
The syntax of the break statement is:
break;
example
// program to print the value of i
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
// break condiTon
if (i == 3) {
break;
}
cout << i << endl;
}
return 0;
}
output
1
2
conTnue stmt:-
n computer programming, the conTnue statement is used to
skip the current iteraTon of the loop and the control of the
program goes to the next iteraTon.
The syntax of the conTnue statement is:
conTnue;
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
// condiTon to conTnue
if (i == 3) {
conTnue;
}
return 0;
}
Output
1
2
4
5
exit stmt
int main () {
for( ; ; ) {
prinâ("This loop will run forever.\n");
}
return 0;
}
example of for loop
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
cout << " Good morning \n";
}
return 0;
}
example of while loop
22
You are eligible to vote!
return 0;
}
#include <iostream>
using namespace std;
inline int cube(int x)
{
return x*x*x;
}
int main()
{
cout << "Our cube is: " << cube(3) << "\n";
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int i, j;
for(i=0; i<6; i++)
{
for(j=0; j<=i; j++)
cout<<"* ";
cout<<endl;
}
cout<<endl;
return 0;
}
output