Thursday, August 31, 2017

Friend function in c++

A friend function is a function defined outside of C++ class. But it has the right to access all private and protected members of the class. Prototypes for friend functions appear in the class definition. The friends are not member functions.
Use of Friend function in C++
#include <iostream>
using namespace std;

class Student {
   double percen;
public:
   friend void printPercen( Student s1 );
   void setPercen( double per );
};

void Student::setPercen( double per ) {
   percen = per;
}

// printPercen() is not a member function of any class.
void printPercen( Student s1 ) {

/*printPercen() is a friend of Student class,hence can access any member of Student class*/

   cout << "Percentage of s1: " << s1.percen;
}

int main( ) {
   Student s1;
   s1.setPercen(90.5);
   // Use friend function to print the percentage.
   printPercen( s1 );
   return 0;
}

Output of program:

Percentage of s1: 90.5

Sunday, August 20, 2017

Fahrenheit to Celsius using Class


Program to convert Fahrenheit to Celsius using Class.

#include<iostream>
using namespace std;
class Temperature
{
   float f,c;

   public:
      void  readFahrenheit(){
cout<<"Enter Fahrenheit degree: ";
cin>>f;
     }
      void convertToCelsius(void){
//float v;
c = (f-32)/1.8;
cout<<"Celsius Degree = "<<c;      
      }
};

int  main()
{
Temperature c;
c.readFahrenheit();
c.convertToCelsius();
return 0;
}

Output of program

Enter Fahrenheit degree: 200
Celsius Degree = 93.3333

Tuesday, August 15, 2017

C++ String class - functions.

This C++ program will demonstrate use of String class - functions. #include <iostream>
#include <string>

using namespace std;

int main () {
   int  len;
   string str1="Hello", str2="World", str;
   
   //String copy function: str1 into str
   str = str1;
   cout << "Content in str = "<< str <<endl;

   //String concatenate: str1 and str2
   str = str1 + str2;
   cout << "Content in str = "<< str <<endl;

   //Total lenghth of str1 after strcat()
   len = str.size();
   cout << "Length of str: " << len <<endl;

   return 0;
}

Output of program 

Content in str = Hello
Content in str = HelloWorld
Length of str: 10

C-style C++ String processing

This program will demonstrate use of C++ string processing using C-style string functions.

#include <iostream>
#include <cstring>

using namespace std;

int main () {
   int  len;
   char str1[10]="Hello", str2[10]="World", str[20];
   
   //String copy function: str1 into str
   strcpy( str, str1);
   cout << "Output of strcpy(str,str1): "<< str <<endl;

   //String concatenate: str1 and str2
   strcat( str1, str2);
   cout << "Output of strcat(str1,str2): "<< str1 <<endl;

   //Total lenghth of str1 after strcat()
   len = strlen(str1);
   cout << "Length of str1: " << len <<endl;

   return 0;
}

Output of Program 

Output of strcpy(str,str1): Hello
Output of strcat(str1,str2): HelloWorld
Length of str1: 10

Monday, August 14, 2017

String processing in C++

This program will find length of string.

#include <iostream>
using namespace std;

int main()
{
    string myStr = "Hello Students";
    cout << "myStr String Length = " << myStr.length();

    return 0;
}

Output of program

myStr String Length = 14

* * *

Diamond Pattern


Following C program will print diamond pattern for number n.

#include<stdio.h>
int main(){
  int i, j, n=5;
  for(i=0; i<n; i++)
  {
    for(j=0; j<n-1-i; j++)
printf(" ");
    for(j=0; j<i; j++)
printf("* ");
printf("\n");
  }

  for(i=n-2; i>0; i--)
  {
    for(j=0; j<n-1-i; j++)
printf(" ");
    for(j=0; j<i; j++)
printf("* ");
    printf("\n");
  }
  return 0;
}

Output of program

   *
  * *
 * * *
* * * *
 * * *
  * *
   *
   

Tuesday, August 1, 2017

C++ program to create student class

This C++ program will create a student class. Program will read and print details of N students . 


#include <iostream>
using namespace std;

class student
{
  private:
        int   rollno;
        char  name[20];
        float percentage;
  public:
    //member function to read details
    void readDetails(){
    cout << "Enter Roll number:";
    cin >> rollno;
   cout << "Enter Name:";
   cin >> name;
   cout << "Enter Percentage:";
   cin >> percentage;
     }
    //member function to display details
    void printDetails(){
    cout << "Student details:\n";
    cout << "RollNo=" << rollno;
cout << ", Name=" << name;
cout << ", Percentage=" << percentage;    
    }
};

int main()
{
    student std[5];    //array of students object
    int n, i;
     
    cout << "Enter total number of students: ";
    cin >> n;
     
    for(i=0; i<n; i++){
        cout << "Enter details of student " << i+1 << ":\n";
        std[i].readDetails();
    }
    
    for(i=0; i<n; i++){
        cout << "\nDetails of student " << i+1 << ":\n";
        std[i].printDetails();
    }
    return 0;
}

Output of program

Enter total number of students: 2
Enter details of student 1:
Enter Roll number:101
Enter Name:AAA
Enter Percentage:85.5
Enter details of student 2:
Enter Roll number:102
Enter Name:BBB
Enter Percentage:90.55

Details of student 1:
Student details:
RollNo=101, Name=AAA, Percentage=85.5

Details of student 2:
Student details:
RollNo=102, Name=BBB, Percentage=90.55