Sunday, February 25, 2018

C++ program : Polymorphism and Virtual Functions

#include <iostream>
using namespace std;
class Base {
public:
    virtual void show() = 0;
};
class Derived : public Base {
    int i;
public:
    Derived(int num) : i(num) { i = i * 2; }
void show(){cout<<i;}




};
int main() {
    int n;
    cin >> n;
    Derived d(n);
    Base &b = d;
    b.show();
    return 0;
}

Thanks
Happy Computing !

C++ : Namespace concept with example program

#include <iostream>
using namespace std;
namespace first
{
int x = 17;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 19.4635;
}
int main() {
using first::x; // Fill the gap
using second::y; // Fill the gap
cin >> x ;
cin >> y ;
cout << x << endl;
cout << y << endl;
cout << first::y << endl;
cout << second::x << endl;
return 0;
}

Thanks
Happy Computing !

C++ : Friend Function and Friend Class example program

#include <iostream>
using namespace std;
class Date
{
int da; // 2 digit day
int mo; // 2 digit month
int yr; // 4 digit year
public:
Date(int d, int m, int y): da(d), mo(m), yr(y) { }
friend ostream& operator<<(ostream& os, const Date& dt); // Fill the return type
};
ostream& operator<<(ostream& os, const Date& dt) // Fill the return type
{
os << dt.mo<< "/" << dt.da<<"/"<< dt.yr<< endl; // Fill the implementation
return os;
}

int main()
{
    int day = 0, month = 0, year = 0;
    cin >> day;
    cin >> month;
    cin >> year;
    Date dt(day, month, year);
    cout << dt;
    return 0;
}

C++ : Operator overloading example program

#include <iostream>
using namespace std;
class Box
{
private:
double length, breadth, height;
public:
Box(double a = 0, double b = 0, double c = 0) :
length(a), breadth(b), height(c) {};
double getDimension(){
return length + breadth + height;
}
Box operator+(const Box& x){ // Fill tme of the func
Box box;
 box.length= x.length + length;// Fill the statement
box.breadth= x.breadth + breadth; // Fill the statement
box.height= x.height + height;// Fill the statement
return box;
}
};
int main(){
double l = 0, b = 0, h = 0;
cin >> l; cin >> b; cin >> h;
Box Box1(5.00, 6.00, 7.00), Box2(l, b, h), Box3;
Box3 = Box1 + Box2;
double dim = Box3.getDimension();
cout << dim;
return 0;
}

Thanks
Happy Computing !

C++ program : IDGenerator

#include<iostream>
using namespace std ;
class IDGenerator {
private:
static int s_nextID;
public:
static int getNextID(); // Fill in the keyword
};
int IDGenerator::s_nextID=0;// Fill in the statement
int IDGenerator::getNextID() { return s_nextID++; }
int main() {
int add = 0, count = 0;
cin >> count;
for (int i = 0; i < count; ++i){
add = add + IDGenerator::getNextID();
}
cout << add;
return 0;
}

Thanks
Happy Computing !

C progran :To reverse the digits of N. Let us denote this number by R. You must output N+R.

#include<stdio.h>

int main()
{
  int num;
  int rev_num = 0, R=0,s=0;
  int N,t;
 
  scanf("%d",&num);
  N=num;
  t=num;
    while(t !=0)
    {
        rev_num = rev_num*10 + t%10;
        t = t/10;
    }
     R=rev_num;
    s=N+R;
   printf("%d",s);
}

Thanks
Happy Computing !

C program :To output the number of matching positions if s is an anagram of t, and -1 if s is not an anagram of t.

We say that a string 's' is an anagram of another string 't' if the
letters in 's' can be rearranged to form 't'.

For example, "butterfly" is an anagram of "flutterby", since a
rearrangement of the first word results in the second.

We say that a position 'i' in 's' and 't' match, if 's' is an anagram of
't', and s[i]==t[i].

In this question, you will be given two words, 's' and 't'. You have
to output the number of matching positions if s is an anagram of t,
and -1 if s is not an anagram of t.

Sample Input 1

butterfly
flutterby

Sample Output 1
2

#include<stdio.h>
#include<string.h>
int main()

{
    char s1[100],s2[100];
    //int num1[26] = {0}, num2[26] = {0}, i = 0;
    int count=0,i=0;
    scanf("%s %s", s1,s2);
      int num1[26] = {0}, num2[26] = {0};

    while (s1[i] != '\0')

    {

        num1[s1[i] - 'a']++;

        i++;

    }

    i = 0;

    while (s2[i] != '\0')

    {

        num2[s2[i] -'a']++;

        i++;

    }

 
 
   
 
    for (i = 0; i < strlen(s1); i++)

    {

        if (num1[i] != num2[i])

        { 
            printf("-1");
            goto A;
         
        }
    }
     
    for (i = 0; i < strlen(s1); i++)

    {

        if (s1[i] == s2[i])

        {
            count++;
         
        }
    }
 
  printf("%d",count);
   
   A: return 0;
}

Thanks
Happy Computing !

what is crystal field theory and why do we need it?

  Calculating CFSE