Assignment Help, Cloud Based ERP System, Microsoft NAV Certification
WELCOME !!

Please Register, ask for assignment solutions & post the solutions if you know any.

LETS START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.

Assignment Help, Cloud Based ERP System, Microsoft NAV Certification
WELCOME !!

Please Register, ask for assignment solutions & post the solutions if you know any.

LETS START POSTING YOUR IDEAS AND THOUGHTS AND BUILD THE COMMUNITY OF EXPERTS.

Assignment Help, Cloud Based ERP System, Microsoft NAV Certification

Stock Market, Online Tutoring, Cloud Based ERP System, Microsoft Dynamics Reporting, Microsoft Nav Certification


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Learn c++ program (Covers 10 chapters) Empty Learn c++ program (Covers 10 chapters) 26th May 2009, 10:49 pm

aparichit

aparichit



Last edited by aparichit on 27th May 2009, 12:42 am; edited 11 times in total

aparichit

aparichit
Code:

/*How do you initialize array of objects which is of class having argumented constructors?*/
#include <iostream>
using namespace std;
struct try1
{
private:
 int try_x,try_y;
 public:
  try1(){}
  try1(int x,int y)
  {
  try_x=x;
  try_y=y;
  }
 
  void display()
  {
  cout << "x="<<<"\t"<<
  }
};
int main()
{
try1 jot_ko[2]={try1(0,1),try1(1,3)};
for (int i=0;i<2;i++)
 jot_ko[i].display();
return 0;
}



Last edited by aparichit on 26th May 2009, 11:26 pm; edited 4 times in total

aparichit

aparichit
Code:
/* Program to add two objects using constant member function
 Constant member function is a function which do not have any rights to modify the data members of a class.

Syntax for Constant member function is          return_type Function_name(arguments) const;
*/

#include <iostream>

using namespace std;

class Addition{
    int feet;
    float inches;
    public:
        Addition(){ // Default Constructor
            feet=5; // Initializing the value of feet and inches to 5
            inches=5;
        }
        void get_distance(){    // Function to take distances from the user
            cout<<"Enter Feet"<
            cin>>feet;
            cout<<"Enter Inches"<
            cin>>inches;
        }
        void show_distance()
        { 
 // Function to show distances when the function is called
            cout<<"After Addition of the two objects:"<
            cout<<"Distance = "<<<" feet "<<<" inches"<
        }
        Addition add(Addition) const;    // Declaration of constant member function
};

Addition Addition::add(Addition A)const
{  // Defination of constant member function
    Addition B;
    //feet+=A.feet; 
  /* Cannot modify feet as it is data member of class and the function is constant.*/
    B.feet=feet+A.feet;
    B.inches=inches+A.inches;    // Here the Data member of class is not modified
    while(1){
        if(B.inches<12)
            break;
        B.inches-=12;
        B.feet++;
    }
    return B;
}

int main(){
    Addition A1,A2,A3;  // Default constructor called
    A1.get_distance();      // Function call to get distance in A1
    A2.get_distance();      // Function call to get distance in A2
    A3=A1.add(A2);          // Function call to add members of A1 and A2 and assign in A3
    A3.show_distance(); // Show the distance assigned in A3
    getchar();
    return 0;
}

aparichit

aparichit
Code:
[b]// Static Data members are those Data members that are accessed once at the first function call and then the line is not compiled next
// Program to illustrate the use of static data members

// This program finds the sum of varialbles entered by user until he/she enters zero

#include <iostream>

using namespace std;

float count(int x){
    static int i=0; // Static data members initializes to 0
    static int sum=0;
    sum+=x;
    if(x==0){
        cout<<endl<<"The total Sum is "<<sum<<endl;
        cout<<endl<<"The average is "<<float(sum)/i<<endl;

    }
    i++;
    return i;
}

int main(){
    int num,total;
    do{
        cout<<"Enter number"<<endl;
        cin>>num;  // Taking the data from the user
        total=count(num);  // Sending the data to the function to calculate the sum
    }while(num!=0);
    cout<<endl<<"Total number of numbers entered = "<<total-1<<endl;
    getchar();
    return 0;
}

// this pointer
// This is a program that uses this pointer to return the address of the calling object.

// Syntax to return the calling function            return *this;

#include <iostream>
#include <cstring>

using namespace std;

class Employee{
    char name[25];
    long int salary;
    char keyboard_buffer[2];
    public:
        Employee(){}    // Default Constructor
        void get_information(){    // Function defination to get information of the object
            cout<<"Enter Name of Employee"<<endl;
            cin.getline(name,20);
            cout<<"Enter salary"<<endl;
            cin>>salary;
            cin.getline(keyboard_buffer,2);    // To avoid assigning ENTER in the next name variable " NOT NECESSARY IN EXAM"
        }
        void show_information(){    // Function defination to show the information of object having higher salary
            cout<<endl<<"Employee having higher Salary is:"<<endl;
            cout<<"Name\t\t"<<name<<endl;
            cout<<"Salary\t\tRs. "<<salary<<endl;
        }
        Employee compare(Employee); // Function to compare the salary of employees
};

Employee Employee::compare(Employee em){        // Function defination of comparing function
    Employee EM;
    Employee em1;
    if(salary<em.salary)
        return em;  // Returns object em in case of salary of em is higher
    if(salary>em.salary)
        return *this;      // Returns the calling object if calling object has the higher salary than the passing object
    else{
        strcpy(EM.name,"Sorry Salary are equal");  // If salary of both employees are same then the message is displayed
        EM.salary=salary;
        return EM;
    }
}

int main(){
    Employee em1,em2,em3;
    em1.get_information();      // Gets information of object em1
    em2.get_information();      // Gets information of object em2
    em3=em1.compare(em2);      // Function call to compare the salary of two objects
    em3.show_information(); // Displays the information of employee of higher salary
    getchar();
    return 0;
}

[/b]

aparichit

aparichit
Code:
// Overriding member function means:
//  Using member function in a derived class that have the same name as those in base class.

#include <iostream>
#include <cstring>

using namespace std;

class base{ // Base class
    protected:
        enum {arr=3};  // defining constant of integer type. for this purpose enum is used
        char name[arr][20]; // array of size 20 and can take arr(3) argument
        int num;
            public:
                base(){
                    num=-1;
                }
                void enter(char str[]){    // function which takes a string argument in name[0], name[1] and name[2]
                    strcpy(name[++num],str);
                }
                char* get(){    // function which returns string argument as name[2], name[1] and name[0]
                    return name[num--];
                }
};

class derived: public base{ // Derived class
    public:
        void enter(char str[]){ // function having same name as that in base class and does the same thing
            if (num>=arr-1){        // concept of function overriding
                cout<<"Nothing To Enter"<<endl;
                exit(1);
            }
            base::enter(str);
        }
        char* get(){    // function having same name as that in base class and does the same thing
            if(num<0){  // concept of function overriding
                cout<<"Nothing To Print"<<endl;
                exit(1);
            }
            return base::get();
        }
};

int main(){
    derived obj;    // Default construcot called
    obj.enter("Pradeep");
    obj.enter("Sudeep");
    obj.enter("Rohit");
    http://obj.enter("shyam");  // if this line is active then error message is displayed
    cout<<obj.get()<<endl;  // Displays the last enterd element
    cout<<obj.get()<<endl;  // Displays the second last entered element
    cout<<obj.get()<<endl;  // Displays the first entered element
    getchar();
    return 0;
}

aparichit

aparichit
Code:
// Static Function is used to access static variables
// Static Function cannot access other variables

// Syntax for static function          static return_type Function_name(arguments);

#include <iostream>
#include <math.h>

using namespace std;

class square{
    static double number;  // Declaration of Static Variable
    static int i;      // Declaration of Static Variable
    int j;
    public:
        square(){  // Constructo that squares the value of 'i' and then increases it by1
            number=pow(i,2);
            j=i;
            i++;
        }
        static void show_number(){  // Function that shows the number when function is called
            cout<<"The number is "<<number<<endl;  // Static Function using only the static variable
        }
        void show_sequence(){  // Function to show sequence when function is called
            cout<<j<<"\t";
        }
};

double square::number=1;      // Declaration of Static Variable 'number' to 1
int square::i=1;            // Declaration Static Variable 'i' to 1

int main(){
    square obj1;    // constructor is called and the square of 1 is assigned to number
    square::show_number();  // Function call to show number i.e, it displays 1
    square obj2,obj3;  // Constructor is called and square of 2 and 3 is assigned in obj2 and obj3 respectively
    square::show_number();  // Function call to show number i.e, id displays 9(squar of 3)
    cout<<endl<<"The sequence is:"<<endl;
    obj1.show_sequence();  // Function call to show sequence of obj1 i.e, 1
    obj2.show_sequence();  // Function call to show sequence of obj1 i.e, 2
    obj3.show_sequence();  // Function call to show sequence of obj1 i.e, 3
    getchar();
    return 0;
}

Formatted

Formatted
Administrator
// Program to show the use of Virtual Base Class
// Base Class are made virtual in order to prevent form ambiguity

// Here if the Base Class was not made virtual by the Derived class then the following this would have happened
// 1. The branch class takes one copy of information of leaves class
// 2. The stem class also takes one copy of information of leaves class
// 3. The branch and stem class is made public to root class and takes each copy of information from branch and stem class.
// 4. When the function get_information() is called then there occurs an ambiguity
// 5. Ambiguity of which information to show as the root contains two infromation each from stem and branch class

Code:

#include <iostream>
#include <cstring>

using namespace std;

class leaves{
    protected:  // data types must be made protected to be accessed in public inheritance
        char information[30];
        public:
            leaves(){  // Default constructor which assigns the string in 'information'
                strcpy(information,"Provide me water to make food");
            }
};

class branch : virtual public leaves{};        // This class makes the Base class virtual
class stem : virtual public leaves{};          // This class also makes the Base class virtual
// if branch and stem classes are not made virtual then ambiguity occurs
class root : public branch, public stem{
    public:
    root(){}
    char* get_information(){
        return information;    // No ambiguity because it return only one copy of leavees information due to virtual base class
    }
};

int main(){
    root R; // Default constructor of root class is called
    cout<<R.get_information()<<endl;    // information of leaves class is inherited by root class and then displayed
    getchar();
    return 0;
}

http://kantipur.friendhood.net

Formatted

Formatted
Administrator
// Program to illustrate the use of virtual base class

Code:
#include <iostream>

using namespace std;

class grand_father{ // Base class
    protected:  // all data members must be made protected to acees in case of public inheritance
        char g_name[20];    // Base class data members
        char g_profession[20];
        public:
            grand_father(){ // constructor which assign the strings in 'g_name' and 'g_profession'
                strcpy(g_name,"Parshuram Subedi");
                strcpy(g_profession,"Ex. British Army");
            }
};

// derived class
class father: virtual public grand_father{  // Base class is made virtual in this class
    protected:  // all data members must be made protected to acees in case of public inheritance
        char f_name[20];    // Derived class data members
        char f_profession[30];
        public:
            father(){  // constructor which assigns the strings in 'f_name' and 'f_profession'
                strcpy(f_name,"Hari Prasad Subedi");
                strcpy(f_profession,"Social Worker (N.R.C.S)");
            }
};

// derived class
class mother: virtual public grand_father{  // Base class is made virtual here too
    protected:  // all data members must be made protected to acees in case of public inheritance
        char m_name[20];    // Derived class data members
        char m_profession[20];
        public:
            mother(){  // constructor which assigns strings in 'm_name' and 'm_profession'
                strcpy(m_name,"Bhawani Devi Subedi");
                strcpy(m_profession,"House wife");
            }
};

// accessing class
class myself: public father, public mother{ // both derived class are made public in this class
    char name[20];                          // so that it can access the data members of both derived classes
    char profession[20];                    // as well as base class as both derived class had made the base class public & (virtual)
    public:
        myself(){  // constructor which assigns strings in 'name' and 'profession'
            strcpy(name,"Rohit Subedi");
            strcpy(profession,"Student");
        }
        void show_information(){    // Function to display infromation when function is called
            cout<<"Grand Father"<<endl<<"------------"<<endl<<"Name\t\t"<<g_name<<endl<<"Profession\t"<<g_profession<<endl; // Base class members accessed
            cout<<endl<<endl<<"Father"<<endl<<"------"<<endl<<"Name\t\t"<<f_name<<endl<<"Profession\t"<<f_profession<<endl; // derived class members accessed
            cout<<endl<<endl<<"Mother"<<endl<<"------"<<endl<<"Name\t\t"<<m_name<<endl<<"Profession\t"<<m_profession<<endl; // derived class members accessed
            cout<<endl<<endl<<"Myself"<<endl<<"------"<<endl<<"Name\t\t"<<name<<endl<<"Profession\t"<<profession<<endl;
        }
};

int main(){
    myself m;  // constructo of myself class is called. As every class are public so constructor of every class are called
                // and name and profession of each class are assigned
    m.show_information();  // function call to display information of object 'm'
    getchar();
    return 0;
}

// if both derived class have not made the base class virtual then there will be the case of ambiguity
// as the base class members are accessed by derived class and the accessing class will get confused in the base class member accessing
// as it derives two base class members.

http://kantipur.friendhood.net

Formatted

Formatted
Administrator
// Program that gives the information of Virtual class and how it is used
// Virtual class must be made in the base class and same function must be there in all derived class

// Syntax for the virtual class is virtual return_type Function_name()

// This Program takes information of Properietor and Employee of different class
// And then display the content of the object of those class by virtual funtion

// pointer of base class pointed to derived class calls the virtual function of that derived class


Code:
#include <iostream>

using namespace std;

class people{      // Abstract Class ( Base Class ) as its object has not been created
    public:
        virtual void show(){        // Virtual Function named show()
            cout<<endl<<"Sorry No Information"<<endl;
        }
        // virtual void show()==0      // Pure virtual function
};

class Properietor : public people{      // Derived Class One
    char name[20];
    double property;
    public:
        Properietor(){} // Default constructor
        void get_data(){    // Function to get information from the user
            cout<<"Enter Name of Properietor"<<endl;
            cin.getline(name,20);
            cout<<"Enter Property"<<endl;
            cin>>property;
        }
        void show(){// Contains same function of virtual class in Base Class which shows the information when function is called
            cout<<endl<<"Properietor Information";
            cout<<endl<<"Name\t\t"<<name<<endl;
            cout<<"Property\tRs. "<<property<<endl;
        }
};

class employee : public people{ // Derived Class Two
    char name[20];
    long int salary;
    char keyboard_buffer[2];
    public:
        employee(){}    // Default constructor
        void get_information(){ // Function to get information from the user
            cin.getline(keyboard_buffer,2); // To avoid assigning 'enter' in name of employee "NOT NECESSARY IN EXAM"
            cout<<endl<<"Enter Name of Employee"<<endl;
            cin.getline(name,20);
            cout<<"Enter Salary"<<endl;
            cin>>salary;
        }
        void show(){// Contains same function of virtual class in Base Class which shows the information when function is called
            cout<<endl<<"Employee Information";
            cout<<endl<<"Name\t\t"<<name<<endl;
            cout<<"salary\t\tRs. "<<salary<<endl;
        }
};

int main(){
    people* ptr;    // Base Class Pointer
    Properietor PR; // Default constructor of properietor class is called
    employee EM;    // Default constructor of employee class is called

    PR.get_data();  // Function call to get information of properietor
    EM.get_information();  // Function call to get infromation of employee

    ptr = &PR;      // Base Class Pointer pointing the address of Derived Class One
    ptr->show();    // Function named show() of Derived Class One is called

    ptr = &EM;      // Base Class Pointer pointing the address of Derived Class Two
    ptr->show();    // Function named show() of Derived Class Two is called
    // If the function in base class is not made virtual then both the times (i.e, ptr->show) the function in base class is called
    getchar();
    return 0;
}

http://kantipur.friendhood.net

Formatted

Formatted
Administrator
// Program to show the constructor and destructor are displayed in reversed order

Code:
#include <iostream>

using namespace std;

class Base{
    public:
        Base(){ // constructor of base class
            cout<<"Constructor of Base Class"<<endl;
        }
        ~Base(){    // Destructor of base class
            cout<<"Destructor of Base Class"<<endl;
        }
};

class Derived1:public Base{
    public:
        Derived1(){ // constructor of derived1 class
            cout<<"Constructor of Derived1 Class"<<endl;
        }
        ~Derived1(){    // Destructor of derived1 class
            cout<<"Destructor of Derived1 Class"<<endl;
        }
};

class Derived2:public Derived1{
    public:
        Derived2(){ // constructor of derived2 class
            cout<<"Constructor of Derived2 Class"<<endl;
        }
        ~Derived2(){    // destructor of derived2 class
            cout<<endl<<"Destructor of Derived2 Class"<<endl;
        }
};

int main(){
    {// scope of object obj starts
    Derived2 obj;
    // when above statement is compiled derived2 class is accessed but derived1 class is public to derived2
    // so compiler flows to derived1 class but base class is public to derived1 class so compiler flows to base class
    // Then constructor of base class is displayed then derived1 constructor and finally derived2 constructor is displayed
    }// scope ends i.e, to say destructor is called
    // Destructor is called in reverse order of that of constructor
    getchar();
    return 0;
}

http://kantipur.friendhood.net

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum