- Get link
- X
- Other Apps
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
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
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
For Complete explanation of C++ please visit - SchoolingAxis
ReplyDelete