3/28/2016

Overloading,friend function,constructor,destructor(mixing operations in c++ )

A program which contents class complex,constructor,destructor,friend function sum,overload arithmetic operators(+ and -).This program accepts two complex numbers and adds or subtracts these two complex number.


#include<iostream>
using namespace std;
class complex
{
public:
float real;
float imag;

complex()
{
real=0;
imag=0;
}

~complex()
{
real=0;
imag=0;
}

complex operator + (complex bbb)
{
complex operation;
operation.real=real+bbb.real;
operation.imag=imag+bbb.imag;
return(operation);
}
complex operator - (complex bbb)
{
complex operation;
operation.real=real-bbb.real;
operation.imag=imag-bbb.imag;
return(operation);
}

void friend sum(complex a,complex b)
{
cout<<a.real;
if(b.imag<0)
{
cout<<"-i"<<(-1)*b.imag;
}
else
{
cout<<"+i"<<b.imag;
}
}
};

int main()
{
int i=0;
char ch;
complex aaa,bbb,operation;
cout<<"Enter first complex no.";
cin>>aaa.real;
cin>>aaa.imag;
cout<<"First complex no.:\n";
sum(aaa,aaa);
cout<<"\nEnter second complex no.";
cin>>bbb.real;
cin>>bbb.imag;
cout<<"Second complex no.:\n"
sum(bbb,bbb);
cout<<"\nSummation:-a\nSubtraction:-b\n";
cin>>ch;
switch(ch)
{
case 'a':
operation=aaa+bbb;
cout<<"\nSummation of two complex no.:\n";

break;

case 'b':
operation=aaa-bbb;
cout<<"Subtraction of two complex no.:\n";

break;
}
sum(operation,operation);
return 0;
}

Output:
Enter fiest complex no.5 -9
First complex no.:
5-i9
Enter second complex no.4 -2
Second complex no.:
4-i2
Summation:-a
Subtration:-b
a
Summation of two complex no.:
9-i11

………………………………………………………
A program using friend function join,overloading assignment operator (=),constructor,destructor.This program accepts two string and joins them.

#include<iostream>
using namespace std;
class str
{
public:
str(){}
~str(){}
char a[50];
void enter()
{
cin>>a;
}

str operator = (str test)
{
for(i=0;i<=49;i++)
{
a[i]=test.a[i];
}
}

void print()
{
cout<<a;
}

void friend join(str aaa,str bbb)
{
string b;
b=strcat(aaa.a,bbb.a);
cout<<"\n\nJoint string="<<b;
}
};

int main()
{
str aa,bb,cc;
cout<<"Enter fitst string:\n";
aa.enter();
bb.opetator= (aa);
cout<<"First string=";
bb.print();
cout<<"\nEnter second string:\n";
cc.enter();
bb.operator= (cc);
cout<<"Second string=";
bb.print();
join(aa,cc);
return 0;
}

Output:
Enter first string:
Very
First string=Very
Enter second string:
good
Second string=good

Joint string=Verygood


----------------------------------------------------------

SEARCH ALL PROGRAMMINGS AND TOPICS






3/19/2016

INLINE MEMBER FUNCTION

INLINE MEMBER FUNCTION
-----------------------------------------------

#include<iostream>
using namespace std;
class test
{
public:
inline void display();
};
inline void test::display()
{
cout<<"WELCOME";
}
int main()
{
test a;
a.display();
return 0;
}

Output:
WELCOME

-----------------------
PLEASE WRITE COMMENTS IN COMMENT BOX
-----------------------




Char and string

1.A C program for counts frequency of letters in given string.


#include <stdio.h>
#include<conio.h>
#include <string.h>

int main()
{
   char string[100];
   int c = 0, count[26] = {0};

   printf("Enter a string\n");
   gets(string);

   while ( string[c] != '\0' )
   {
   

      if ( string[c] >= 'a' && string[c] <= 'z' )
   count[string[c]-'a']++;

      c++;
   }

   for ( c = 0 ; c < 26 ; c++ )
   {
      if( count[c] != 0 )
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }

 getch();
}


Output:
Enter a string
aabbccc
a occurs 2 times in the entered string.
b occurs 2 times in the entered string.
c occurs 3 times in the entered string.

2.String length

#include<stdio.h>
#include<string.h>
int main()
{
  char a[100];
  int length;
  printf("Enter a string to calculate it's length\n");
  gets(a);
length = strlen(a);
/* strlen(string) : function gives length of string*/

  printf("Length of entered string is = %d\n",length);
 return 0;
}
Output:
Enter a string to calculate it's length
welcome
Length of entered string is = 7


3.Compare strings

#include <stdio.h>
#include <string.h>
int main()
{
  char a[100], b[100];
printf("Enter the first string\n");
  gets(a);
printf("Enter the second string\n");
  gets(b);
/*strcmp(string, string) : returns 0 if strings are equal else strings are unequal*/
  if( strcmp(a,b) == 0 )
  printf("Entered strings are equal.\n");
  else
  printf("Entered strings are not equal.\n");
return 0;

}

Output:
Enter the first string
hello
Enter the second string
hello
Entered strings are equal.

4.Copy string

#include<stdio.h>
#include<string.h>
main()
{
  char source[] = "C program";
  char destination[50];

  strcpy(destination, source);
/*strcpy will copy string from source to destination */

  printf("Source string:%s\n", source);
  printf("Destination string:%s\n", destination);

  return 0;
}

Output:
Source string:C program
Destination string:C prograram




5.Concatenate strings

#include<stdio.h>
#include<string.h>

int main()
{
  char a[100], b[100];

  printf("Enter the first string\n");
  gets(a);

  printf("Enter the second string\n");
  gets(b);

  strcat(a,b);
/*strcat will append string b on string a*/

  printf("String obtained on concatenation is \n%s\n",a);

  return 0;
}

Output:
Enter the first string
Ram
Enter the second string
Raj
String obtained on concatenation is 
RamRaj



6.A C program to check either a string is palindrome or not.
Note: Palindromes are words or phrases that read the same in both directions.


#include<stdio.h>
#include<string.h>
#include<conio.h>

char *strrev(char *str)
{
      char *p1, *p2;

      if (! str || ! *str)
            return str;
      for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
      {
            *p1 ^= *p2;
            *p2 ^= *p1;
            *p1 ^= *p2;
      }
      return str;
}

int main()
{
char a[100], b[100];

//Get the number from user.
printf("Enter the string to check if it is a palindrome\n");
gets(a);

//copy input to another array b
strcpy(b,a);

//Reverse it.
strrev(b);

/*Compare it with the number entered by the user.If both are same then print palindrome number*/
if( strcmp(a,b) == 0 )
printf("\nEntered string is a palindrome.\n");
else
printf("\nEntered string is not a palindrome.\n");
getch();
return 0;
}

Output:
Enter a string to check if it is a palindrome
radar

Entered string is a paindrome.



7.A C++ program to change the string in capital letter into small letter.

#include<iostream>
using namespace std;
int main()
{
int i;
char name[100];
char ch;
cout<<"Enter a word in capital letter\n";
cout<<"Enter @ at the last\n";
cin>>name;
while(name[i] != '@')
{
ch=name[i];
ch=ch+32;
cout<<ch;
i++;
}
return 0;
}

Enter a word in capital letter
Enter @ at the last
TEST@
test


8.A C++ program for find the length of a string and reverses the string.


#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;

int main( )
{
clrscr();
char str[80];
int temp, l;
cout<<"Enter string :";
gets(str);

for(l=0;str[l]!='\0';l++);  
 
    cout<<"Length of string:"<<l<<endl;

for(int i=0,j=l-1;i<l/2;i++,j--)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}

cout<<"Reverse String is\n"<<str;

return 0;

}


Output:
Enter string:welcome
Length of string:7
Reverse string is
emoclew



Overloading functions in c++

A program to swap two numbers(either integers or floats) by using overloading function.

#include<iostream>
using namespace std;
class temp
{
public:
void swap(int,int);
void swap(float,float);
temp()
{
cout<<"Choose one option:";
cout<<"\nSwap integers-1\nSwap floats-2\n";
}
};

void temp::swap(int no1,int no2)
{
int temp=no1;
no1=no2;
no2=temp;
cout<<"After swap:"<<no1<<" "<<no2;
}
void temp::swap(float no1,float no2)
{
float temp=no1;
no1=no2;
no2=temp;
cout<<"After swap:"<<no1<<" "<<no2;
}
int main()
{
temp aaa;
int option;
int a,b;
cin>>option;
if(option==1)
{
cout<<"Enter two integers:";
cin>>a>>b;
aaa.swap(a,b);
}
if(option==2)
{
cout<<"Enter two floats:";
foat aa,bb;
cin>>aa>>bb;
aaa.swap(aa,bb);
}
return 0;
}

Output:
Choose one option:
Swap integers-1
Swap floats-2
2
Enter two floats:4.5
6.3
After swap:6.3 4.5


…………………………………………………
…………………………………………………

A program  to find the square of a number(either integer or float) by using function overloading. 

#include<iostream>
using namespace std;
class maths
{
public:
int square(int);
float square(float);
void menu();
};

int maths::square(int a)
{
return(a*a);
}
float maths::square(float a)
{
return(a*a);
}

void maths::menu()
{
cout<<"Choose in the following options:\n";
cout<<Square of an integer no.:1\nSquare of a float no.:2\n";
}
int main()
{
maths aaa;
aaa.menu();
int option;
int x;
float y;
cin>>option;
if(option==1)
{
cout<<"Enter an integer value:";
cin>>x;
cout<<"Square="<<aaa.square(x);
}
if(option==2)
{
cout<<"Enter a float value:";
cin>>y;
cout<<"Square="<<aaa.square(y);
}
return 0;
}

Output:
Choose in the following options:
Square of an integer no.:1
Square of a float no.:2
2
Enter a float value:3.4
Square=11.56



……………………………………………………………………………



Overloading Arithmetic Operators
A program to sum the two complex number.
……………………………………………………………………………

#include<iostream>
using namespace std;
class complex
{
public:
float real;
float imag;
complex operator + (complex bbb)
{
complex sum;
sum.real=real+bbb.real;
sum.imag=imag+bbb.imag;
return(sum);
}
};
int main()
{
complex aaa,bbb,sum;
cout<<"Enter first complex no.";
cin<<aaa.real;
cin>>aaa.imag;
cout<<"Enter second complex no.";
cin>>bbb.real;
cin>>bbb.imag;
sum=aaa+bbb;
cout<<"\nSummation of two complex no.:\n";
cout<<sum.real;
if(sum.imag>=0)
{
cout<<"+i"<<sum.imag;
}
else
{
cout<<"-i"<<(-1)*sum.imag;
return 0;
}

Output:
Enter first complex no.-3 -8
Enter second complex no.5 2

Summation of two complex no.:
2-i6

…………………………………………………………………………

Overloading assignment Oprator
-----------------------------------------------------

#include<iostream>
using namespace std;
class test
{
public:
int a;
test(int num)
{
a=num;
}
test operator = (test aaa)
{
a=aaa.a;
}
void print()
{
cout<<a;
}
};

int main()
{
test aa(4);
test bb(5);
bb.operator= (aa);
cout<<"In object bb,value=";
bb.print();
return 0;
}

Output:
In object bb,value=4


………………………………………………………………………





3/18/2016

FRIEND FUNCTIONS IN C++


Use of friend function is to access the private data member by non member function.

A simple program

#include<iostream>
using namespace std;
class test
{
private:
int a=4;
public:
friend void display(test abc)
{
cout<<"Value of a="<<abc.a;
}
};

int main()
{
test obj;
display obj;
return 0;
}


Output:
Value of a=4;

………………………………………………
………………………………………………

This is the program of the adding of real and imaginary parts of a complex number by using friend function.

#include<iostream>
using namespace std;
class complex
{
private:
int real,imaginary;
public:
void realno()
{
cout<<"Enter real part:";
cin>>real;
}
void imaginaryno()
{
cout<<"Enter imaginary part:";
cin>>imaginary;
}
friend void sum(complex a,complex b)
{
cout<<"Complex no. is: ";
cout<<a.real;
if(b.imaginary<0)
{
cout<<"-i"<<(-1)*b.imaginary;
}
else
{
cout<<"+i"<<b.imaginary;
}
};

int main()
{
complex a,b;
a.realno();
b.imaginaryno();
sum(a,b);
return 0;
}

Output:
Enter real part:3
Enter imaginary part:4
Complex no. is: 3+i4

…………………………………………………
…………………………………………………

A program to join two strings using friend function.

#include<iostream>
#include<string.h>
using namespace std;
class str2;
class str1
{
private:
char a[100];
public:
void getdata()
{
cout<<"Enter a string:";
cin>>a;
}
friend void join(str1,str2);
};

class str2
{
private:
char b[100];
public:
void getdata()
{
cout<<"Enter second string:";
cin>>b;
}
friend void join(str1,str2);
};

void join(str1 aaa,str2 bbb)
{
string c;
c=strcat(aaa.a,bbb.b);
cout<<c;
}

int main()
{
str1 aa;
str2 bb;
aa.getdata();
bb.getdata();
join(aa,bb);
return 0;
}

Output:
Enter a string:Te
Enter second string:st
Test

…………………………………………………
…………………………………………………

FRIEND CLASS



#include<iostream>
using namespace std;
class student1
{
friend class student2;
private:
int marks1=80;
};
class student2:public student1
{
private:
int marks2=70;
public:
void display()
{
cout<<"marks1="<<marks1<<"\nmarks2="<<marks2;
}
};

int main()
{
student2 obj2;
obj2.display();
return 0;
}


Output:
marks1=80
marks2=70

………………………………………………
………………………………………………


CLICK FOR SEARCH TOPIC
                                                 





3/08/2016

THIS POINTER

#include<iostream>
using namespace std;
class test
{
public:
int a,b;
void set()
{
this->a=4;
(*this).b=5;
cout<<(*this).a<<"\n<<this->b;
}
};

int main()
{
test aaa;
aaa.set();
return 0;
}


Output:
4
5

……………………

#include<iostream>
using namespace std;
class aaa
{
public:
int value;
void display()
{
this->value=20;
cout<<"Address:"<<this<<"\n";
}
};
int main()
{
aaa b;
b.display();
return 0;
}

Output of the above program gives the address of the object.



Pointers and Array

Using arrays of pointers find the sum of desired number of integer.

#include<iostream>
using namespace std;
int main()
{
int *num[100];
int i,a,n,sum=0;

cout<<"Enter total number of integers:";
cin>>n;

cout<<"Enter integers:\n";

for(i=0;i<=n-1;i++)
{
cin>>a;
num[i]=&a;
sum=sum+*num[i];
}

cout<<"Sum of integers:"<<sum;

return 0;
}


Output:
Enter total number of integers:3
Enter integers:
3
4
5
Sun of integers:12


…………………………………………………………
…………………………………………………………



A function p which argument is a pointer to an array of characters and which returns a  pointer to an integer.


#include<iostream>
using namespace std;
int main()
{
char ch[10]={'b','c','d'};

int p(char *str);

cout<<"The value of an integer:"<<s(&ch[10]);

return 0;
}

int p(char *ptr)
{
int a=9;
int *num;
num=&a;
return(*num);
}


Output:
The value of an integer:9


……………………………………………………
……………………………………………………

p is a function whose argument is a pointer to char and which returns an element of an array of ten integers.

#include<iostream>
using namespace std;
int main()
{
int p(char *ptr);
char ch='b';
cout<<"Required number:"<<p(&ch);
return 0;
}

int p(char *ptr)
{
int num[10]={1,2,3,4,5,6,7,8,9,10};
int *number;
number=num;
return(number[5]);
}


Output:
Required number:6




3/07/2016

Question bank of c++ programming

(Note-The solutions of the various programmings of this question bank are available at related topic content or 'c++ programming also object oriented programming in c++' page)

BASICS

Difference between c anf c++.

Different batween procedural and Object Oriented Programmimg

Briefly discuss different data types in c++.

Escape sequence

constant

void data types

operator

identifiers

modifiers

character constant and string constant

Discuss the basic data types of C++.Suggest appropriate data type for the following:
(1)Someone's height in meters
(2)An exclamation mark
(3)The number of students in a university

Write a C++ program to find the sum of the series
1+3+5+......+n.

Discuss conditional,logical and increment operators with example.
CONTROL STATEMENT

Discuss the significance of break statement in switch case construct with an example.

Compare and contrast while,do-while and for loop with example.

Explain nested switch() case statrment with an example.Show outputs.

Explain branching statements used in C++ with example.

Syntactically explain nested "if-else" statement in c++.

POINTER AND STRING

Write the expressions to present the following:-
(1) p is a function whose argument is a pointer to an array of characters and which returns a pointer to an integer.
(2) p is a function whose argument is a pointer to character and which returns a pointer to an array of ten integers.

Define string.How does a string type differ from C type string?

WAP to reads a particular string and display the frequency of each character in the string.

WAP in C++ using array of pointers to accept desired number of integers and display their sum.

Explain 'this' pointer,void pointer,wild pointer.





CLASS AND OBJECT

Define class and object.Write a program to calculate  factorial of a no. using class and object.

Differentiate Private,Public,Protected data members of the class using example.

SPECIAL MEMBER FUNCTION
What is the diff. between zero argument constructor and parameterized constructor?Explain with example.

Create a class complex and implement the following:
(1)Define suitable constructor and destructors
(2)Overload the operators + and -
(3)Write a friend function sum which adds.the real and imaginary parts of a complex object

Explain concept of destructor in a class.
What is its role in terms of cleanup of unwanted objects?

What is an inline function?In which situations would you make a function inline?
Give two examples of inline function.Its advantages and disadvantages.

Design and implement a class string  using an array.with a maximum size of 20 characters.The class should contain the necessary constructors,destructor,
overloaded assignment opetator and a friend function for concatenation of two strings.
Make suitable assumptions if required.Also write main() for the above.


Write a program that defines a shape class with a constructor that gives value to width and height. Then define two sub-classes triangle and rectangle, that calculate the area of the shape area().In the main, define two variables a triangle and a rectangle and then call the area() function in this two variables.


Define copy constructor.Explain its significance.Under which conditions is it invoked?Support your answer with an example.



Write a C++ program for linear search using recursive function.

Write a C++ program to find Armstrong number.

Given the C++ code
class A{
public:
A(){a=new int[3];for(int i=0i<3;i++)a[i]=i;}
~A()
{
delete[] a;}
private:
int *a;
};

void int(A  &x) //sets elements of x.a to 0,1,2
{
A y; //fresh object
x=y;
}

int main()
{
A p;

init(p);

}

You might notice that as the program runs following the call init(p),the values stored in p.a stary changing unpredictably.What might be happening?Fix this problem.

VERTUAL FUNCTION ,INHERITANCE,OVERLOADING FUNCTIONS,TEMPLATES
List out the advantages and disadvantages of inheritance.

What is the virtual mamber function in C++?Describe the rules for declaring virtual functions.

What is virtual destructor?How virtual functions call up is maintained?

What heppens if you leave the virtual keyword off the declaration of the member function?Is there an equivalent of a non vertual mamber function.

Diff. between overload function and function templates.

Inheritance and its types.

WAP to demonstrate use of vertual functions.

Explain the constructor and destructor with multi-level inheritence using C++ program.

Define a simple C++ class template myobj,parameterized by a single type,such the following code will work:
myobj<int>s1,s2;
myobj<int>s3=sum(s1,s2,s2);
Make the myobj template as simple as you like.


Write a simple C++ class A that overloads its operators==() method such that the following procedure will work:
void f(A &p,A &q)
{
if(p==q)
{
cout<<"yes\n";
else
cout<<"No\n";
}


Suppose class B was derived from class A such that B de_ned an operator==() to compare two objects of class B,as in
class B:public A{
bool operator==(B &x);
}

Note,though,that in,
int main()
{A a1;
A a2;
B b1;
B b2;
f(a1,a2);//uses A's==operator
f(b1,b2);//also uses A's==operator


Turn the C++ definition
int sum(int a,int b,int c)
{
return a+b+c;
}

into a function template that can be used to work on any type that supports +,instead of just int.Be sure that the template will work for objects that implement the + operator,so that dynamic dispatching of + will occur where possible.

What is operator overloading in C++?Explain with suitable example.Also list operators which cannot be overloaded.

Differentiate between Operator and Function overloading with the help of suitable example.

With relevant examples ,explain:-
(1) multilevel inheritance
(2) hybrid inheritance


What is the output of the following code:-
#include<iostream>
class A{
public:
void f()
{
std::cout<<"A::f"<<std::endl;
}
virtual void g()
{
std::cout<<"A::g"<<std::endl;
}
};
class B:
publicA
{
public:
void f()
{
std::cout<<"B::f"<<std::endl;
}
virtual void g()
{
std::cout<<"B::g"<<std::endl;
}
};
int main(int aegc,char** argv)
{
A a;
B b;
A* aPtr=&a;
A*bPtr=&b;
aPtr->f();
aPtr->g();
bPtr->f();
bPtr->g();
return 0;
}
Is there anything to be noticed?Explain it.

How can a common friend function to two different classes be declared?

Define the following:
Polymorphism
Typecasting
Class and instance
Containership
Abstract class



SEARCH TOPIC FOR ANSWERS
                                                         

-----------
PLEASE WRITE COMMENTS IN COMMENT BOX
-----------