Friday, April 19, 2019

How to Declare a Class in C++

What is Class

An instance of a class is called an object and programs can contain any number of classes. As with other types, object types are case-sensitive. Classes provide encapsulation as defined in the Object Oriented Programming (OOP) paradigm. A class can have both data members and functions members associated with it.


The Basic Syntex of Class in C++

class ClsName
{
private : variables declaration;
function declaration;

public : variables declaration;
function declaration;

protected: variables declaration;
 function declaration;
};

And How we Make an Object in C++

ClsName object;

In Object-oriented programming, an object is an instance of a Class. Objects are an abstraction. They hold both data, and ways to manipulate the data. ... Most applications (and other objects) only change this object through this interface.


A Basic Program of Class in C++

ClsName object;




# include <iostream>
# include <iomanip>
using namespace std;

/*
class ClsName
{
private : variables declaration
function declaration

public : variables declaration;
function declaration;

protected : variables declaration;
function declaration;
};
*/
class Emp{
private:
int id;
char name[30];
char dept[30];

public : 
void GetData(void)
{
cout<<"Enter Employee Id: \n";
cin>>id;

cout<<"Enter Name: \n";
cin>>name;

cout<<"Enter department: \n";
cin>>dept;

cout<<"\n\n";
}

void DisInforn(void)
{
cout<<"\n\n----------------------------------------\n\n";
cout<<"Employee Id: "<<id<<endl;
cout<<"Employee Name: "<<name<<endl;
cout<<"Employee Department: "<<dept<<endl;
}
};
int main() {
Emp obj1;
Emp obj2;
Emp obj3;
Emp obj4;
Emp obj5;
/*---------------------------*/

cout<<"Enter Employee Basic Details \n";
obj1.GetData();
obj2.GetData();
obj3.GetData();
obj4.GetData();
obj5.GetData();

cout<<"Employees Basic Details \n";

obj1.DisInforn();
obj2.DisInforn();
obj3.DisInforn();
obj4.DisInforn();
obj5.DisInforn();

return 0;

How to Use Nested If Else in C++

What is Nested If Else


The dropping else is a problem in computer programming in which an optional else clause in an if-then(–else) statement results in nested conditionals being ambiguous. Formally, the reference context-free grammar of the language is ambiguous, meaning there is more than one correct parse tree.

In many programming languages one may write conditionally executed code in two forms: the if-then form and the if-then-else form – the else clause is optional:
if a then s
if b then s1 else s2


Program of Nested If Else


#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int a,b,c,d;
// Process
cout<<"Enter 4 Numbers\n";
cin>>a>>b>>c>>d;

if(a > b){
if (a > c){
if (a > d){
cout<<"Largest no is:- "<<a<<endl;
} else {
cout<<"Largest no is:- "<<d<<endl;
}
} else {
if(c>d){
cout<<"Largest no is:- "<<c<<endl;
} else {
cout<<"Largest no is:- "<<d<<endl;
}
}
} else {
if(b > c) {
if (b > d){
cout<<"Largest no is:- "<<b<<endl;
} else{
cout<<"Largest no is:- "<<d<<endl;
}
} else {
if (c > d){
cout<<"Largest no is:- "<<c<<endl;
} else {
cout<<"Largest no is:- "<<d<<endl;
}
}
}

 return 0;
}

Output


Enter 4 Numbers
5
6
8
1
Largest no is:- 8

Friend Function in C++

What is a Friend Function


In object-oriented programming, a friend function, that is a "friend" of a given class, is a function that is given the same access as methods to private and protected data.

A friend function is declared by the class that is granting access, so friend functions are part of the class interface, like methods. Friend functions allow alternative syntax to use objects, for instance f(x) instead of x.f(), or g(x,y) instead of x.g(y). Friend functions have the same implications on encapsulation as methods.

Program of Friend Function

#include <iostream>
#define MAX_SIZE 100
using namespace std;

class sum{
private:
int num[MAX_SIZE];
int n;
public:
void GetNum(void);
friend int add(void);
};

void sum::GetNum(void){
cout<<"\nEnter the total number (n)\n";
cin>>n;
cout<<"\nEnter the number (n)\n";
for(int i = 0; i<n;i++){
cin>>num[i];
}
}
// Friend
int add(void){
sum obj;
int temp = 0;
obj.GetNum();
for (int i = 0; i < obj.n;i++){
temp +=obj.num[i];
}
return temp;
}
int main(){
int res;
res = add();
cout<<"The sum of n value is= "<<res<<endl;
return 0;
}

Output

Enter the total number (n)
6
Enter the number (n)
8
4
5
2
8
7
The sum of n value is= 34

How to Use Member Function C++ (For And Max Value)

What Is Member Function C++? 

A class in C++ is a user-defined type or data structure declared with keyword class that has data and functions (also called methods) as its members whose access is governed by the three access specifiers private, protected or public (by default access to members of a class is private). The private members are not accessible outside the class; they can be accessed only through methods of the class. The public members form an interface to the class and are accessible outside the class.

Instances of a class data type are known as objects and can contain member variables, constants, member functions, and overloaded operators defined by the programmer.


Program of Member Function C++ (For And Max Value)


 #include <iostream>
using namespace std;
class Number{
int x,y,z;
public:
void get_data(void);
void maximum(void);
void minumum(void);
};
void Number::minumum(void){
int min;
min = x;
if (min>y)
min = y;
if (min>z)
min = z;
cout<<"\n Minmum value is= "<<min<<endl;
}
// Member Function
void Number::get_data(void){
cout<<"Enter value of 1st num :- ";
cin>>x;
cout<<"Enter value of 2nd num :- ";
cin>>y;
cout<<"Enter value of 3rd num :- ";
cin>>z;
}

// Member Function 2nd
void Number::maximum(void){
int max;
max = x;
if(max<y)
max = y;
if (max < z)
max = z;
cout<<"\n Maximum value is= "<<max<<endl;
}

int main()
{
Number num;
num.get_data();
num.minumum();
num.maximum();
return 0;
}


Output

Enter value of 1st num :- 6
Enter value of 2nd num :- 3
Enter value of 3rd num :- 6
 Minmum value is= 3
 Maximum value is= 6

How to make Pyramid in C++ Using Loop


Making Pyramid of Stars Numbers 

#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int row, i, j;

 cout<<"Enter Number of row : - ";
 cin>>row;
for (i = row; i >=1; --i){
for(j = 1; j <= i; ++j){
cout<<"* ";
}
cout<<"\n";
}
return 0;
}

Output

Enter Number of row : - 5
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


Making Pyramid of Stars * 

#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int row, i, j;

 cout<<"Enter Number of row : - ";
 cin>>row;
for (i = row; i >=1; --i){
for(j = 1; j <= i; ++j){
cout<<"* ";
}
cout<<"\n";
}
return 0;
}

Output

Enter Number of row : - 5
* * * * *
* * * *
* * *
* *
*


Making Pyramid of Stars Alphabet 

#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 char input, alp = 'A';
 int i, j;


 cout<<"Enter Char you wanna Print : - ";
for (i = 1; i <=(input-'A'+1); ++i){
 cin>>input;


  for(j = 1; j <= i; ++j){
  cout<<alp<<" ";
 }
 ++alp;
 cout<<"\n";
}
return 0;
}

Output


Enter Char you wanna Print : - G
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G

Loop in C++ | For Loop, While Loop, and Do While Loop

What is a loop in C++

In c++ programming languages, a do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block.

The do while construct consists of a process symbol and a condition. First, the code within the block is executed, and then the condition is evaluated. If the condition is true the code within the block is executed again. This repeats until the condition becomes false. Because do while loops check the condition after the block is executed, the control structure is often also known as a post-test loop. Contrast with the while loop, which tests the condition before the code within the block is executed, the do-while loop is an exit-condition loop. This means that the code must always be executed first and then the expression or test condition is evaluated. If it is true, the code executes the body of the loop again. This process is repeated as long as the expression evaluates to true. If the expression is false, the loop terminates and control transfers to the statement following the do-while loop. In other words, whereas a while loop sets the truth of a statement as a condition precedent for the code's execution, a do-while loop provides for the action's ongoing execution subject to defeasance by the condition's falsity, which falsity (i.e., the truth of the condition's negation) is set as a condition subsequent.

It is possible, and in some cases desirable, for the condition to always evaluate to true, creating an infinite loop. When such a loop is created intentionally, there is usually another control structure (such as a break statement) that allows termination of the loop.

Some languages may use a different naming convention for this type of loop. For example, the Pascal language has a "repeat until" loop, which continues to run until the control expression is true (and then terminates) — whereas a "while" loop runs while the control expression is true (and terminates once the expression becomes false).



For Loop


#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int num;
 cout<<"Enter The Number: - ";
 cin>>num;
// Process
// For Loop
for (num; num > 0; num--)
{
 cout<<num<<", ";
}
cout<<"End";
 return 0;
}

While Loop

#include<iostream>
#include<string>
using namespace std;
int main(){
// Variables
 int num;

 cout<<"Enter The Number: - ";
 cin>>num;
// Process

// While loop
while(num > 0){
  cout<<num<<", ";
 --num;
 }
cout<<"End";
 return 0;
}

Do While Loop


#include<iostream>
#include<string>
using namespace std;

int main(){
// Variables
 int num;
 cout<<"Enter The Number: - ";
 cin>>num;

// Process

 do{
  cout<<num<<", ";
 --num;
 }
 while(num > 0);

cout<<"End";

 return 0;
}


Output
Enter The Number: - 5
5, 4, 3, 2, 1, End