Skip to main content

How to start learning Web-development

Funcation in c++ programming example in detail

Friend Funcation-

1. Adding to complex number in friend function


#include<iostream>
using namespace std;
class container
{
float a,b;
public:
void get()
{
cout<<endl<<"Enter value for A=";
cin>>a;
cout<<endl<<"Enter value for B=";
cin>>b;
}
void disp()
{
cout<<endl<<a;
cout<<endl<<b;
}
friend container addition(container,container);
};
container addition(container X,container y)
{
container Z;
Z.a=X.a+y.a;
Z.b=X.b+y.b;
return Z;
}
int main()
{
container P,Q,R;
P.get();
P.disp();
Q.get();
Q.disp();
R=addition(P,Q);
R.disp();
return 0;
}

Output -
Enter value for A=12

Enter value for B=12

12
12
Enter value for A=1

Enter value for B=1

1
1
13
13

2.  If class one is friend function of two it not means that two is friend function of one


#include <iostream>
class One
{
private:
int x;

public:
One() { x = 0; }
friend class two; //Friend Class
};

class two
{
private:
int y;

public:
void display(One &p)
{
// private members of One
std::cout << "One::x=" << p.x;
}
};

int main()
{
One x;
two y;
y.display(x);
return 0;
}

Output -
One::x=0

3.  Function overloading with return type 


#include<iostream>
using namespace std;

class addition{
public:
int sum(int A1, int A2) {
return A1+A2;
}
int sum(int A1, int A2, int A3) {
return A1+A2+A3;
}
};

int main(){
addition add;
cout<<add.sum(2,4)<<endl;
cout<<add.sum(2,4,5);
return 0;
}

Output -
6
11

Inheritance function program -


1. program of inheritance 

#include <iostream>
using namespace std;

// Base class
class first
{
public:
int a;
};

// derived class
class second:public first
{
public:
int b;
};

int main()
{
second obj;
obj.b=30;
obj.a=66;
cout<<"second b "<<obj.b<<endl;
cout<<"first a "<<obj.a<<endl;
return 0;
}

// output -->>

// second b 5
// one a 70

Structure program -

It is a user-defined datatype in c/c++ language which allows us to combine data of different data- types together. it helps to construct a complex data type which is more meaningful and It is some how very similar to an Array , but an array holds data of similar type only. But structure on the other hand, can store data of any type like integer , float , double, which is practically more useful.

For example: If I have to write a program to store Student information, which will have Student's name, age, branch, permanent address, father's name etc, which included string values, integer values etc, how can I use arrays for this problem, I will require something which can hold data of different types together.
In structure, data is stored in form of records.

Defining a structure -
struct keyword is used to define a structure. struct defines a new data type which is a collection of primary and derived datatypes

Syntax:
struct [struct_tag]
{
...
}[struct_variables];

Example of Structure -
#include <stdio.h>
#include <string.h>

struct Books {
char title_name[50];
char author_name[50];
char subject[100];
int book_id;
};

void displaybook( struct Books book );

int main( ) {

struct Books Book1;
struct Books Book2;

/* Description */
strcpy( Book1.title_name, "C/C++ Programming");
strcpy( Book1.author_name, "Developer Insider");
strcpy( Book1.subject, "C/C++ Programming Tutorial");
Book1.book_id = 6495407;

/* Description */
strcpy( Book2.title_name, "Python Programming");
strcpy( Book2.author_name, "Developer Insider");
strcpy( Book2.subject, "Python Programming Tutorial");
Book2.book_id = 6495700;

/*Display books detail*/

displaybook( Book1 );

displaybook( Book2 );

return 0;
}

void displaybook( struct Books book ) {

printf( "Book Title : %s\n", book.title_name);
printf( "Book author are : %s\n", book.author_name);
printf( "Book Subject : %s\n", book.subject);
printf( "Book Id : %d\n", book.book_id);
}

Output -

Book Title : C/C++ Programming
Book author are : Developer Insider       
Book Subject : C/C++ Programming Tutorial 
Book Id : 6495407
Book Title : Python Programming
Book author are : Developer Insider       
Book Subject : Python Programming Tutorial
Book Id : 6495700



Comments

Post a Comment

Popular posts from this blog

C++ programming tutorial 2

Control statement in C++ PROGRAMMING - 1. if-else statement :    there are types below of if else statement so they are -     1. if-statement      In this if statement a condition was entered if the condition was match or say what we write in condition on it was true then program was run if it was false then the program in if statement was not run . Syntax - if(Condition){ //code you want to run } PROGRAM - // if-else statement  #include <iostream> using   namespace  std; int   main () {      int   a  ;      cout << "If the entered integer in equal to the condition they it run " << endl ;      cin >>   a ;      if ( a <= 6 )     {          cout << "Program was running" ...

C++ programming tutorial 5

 Call by value or reference - There are two type in function call by value or call by reference in it we pass the value in call by value we pass value and in call by reference we pass the reference of the value . Call by value in C++ Programming language  :- In call by value we pass the variable or say argument and then it used by function which we made so see the code given below  Syntax - #include <iostream > using namespace std; // making function  void fun_name(int a, int b) { ... } int main() { //calling function  fun_name(int n,int n1); return 0; } Code -  // function call by value in C++ programming #include <iostream> using namespace std; // call by value in function declaration void container(int a, int b); int main() {   int x,y;   cout<<"Enter the value of x :"<<endl;   cin>>x;   cout<<"Enter the value of y :"<<endl;   cin>>y;   // here we passing the value as argume...