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

Class and Object , function overloading Explanation in c++ language

 Classes in C language - It is a user defined data type data type which hold its own data members and member function which can be accessed and user by creating an instance of that class and we say that class is building block that leads to Object Oriented programming  Data member -  It is the data variable of the function  Member function - It are the function use to manipulate these function variable  For Example -  A car is an object the car has attribute such as weight , color and methods such as drive and brake Attribute and method are basically variable and function that belong to class syntax - class   class{        // The class    public :              // Access specifier      int   Num;         // Attribute (int variable)     string word;   // Attribute (string variable) }; Program - // class explaination #include <iostream> using   namespace  std; class   classname {      public:      string   mystring ;      void   printfun ()     {          cout   <<   "Gee

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 argument   // and also calling the function  

Code of HTML and CSS how we take color , border , etc

  code of HTML and CSS - 1. How we take change color in css (color and border both property) code - <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>color in css</title> <style> .color1{ background-color: green; } .color2{ color: yellow; } .color3{ border: 2px solid red; } </style> </head> <body> <!-- lets take paragarph --> <p class="color1"> In display property if we apply inline-block property on it we can be set their width and height and element was set next to a element in line it not take extra space so in easy word it is work as both block element who's width and height can be changed </p> <p class="color2"> In display property if we apply inline-blo