MS Rumi. Powered by Blogger.
Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Thursday, 30 October 2014

C++ Project For Novice Programmer Bank Account With Classes

What is Static Variable in c++?

In simple computer codding or in programming, variable that allocated statically called static variable. Static variable saved in memory till program ends . In other words its the variable you define as static and it will store at the same place or location you assign it and it will remain at same place throughout the execution of your whole program.
Static Local Variable
Static Variable in C++
Question Asked in Interview "What is Static?"
Answer:- It is a variable that is declared static in the body of function and it maintains its data or value throughout the function.
Static Variable is declare : static int   totalObject;
Initializing Value, Outside  the class   e.g int Bank :: totalObject=0;  You can check in example below.
Operation: totalObject++; inside the class

What is member Initializer list in c++ and Why need it?

Member intilializer is a constructor used in c++. Instant of using variable inside the constructor here we use it outside the constructor body. Variable are listed after the header of the constructor and it is separated by a colon (:). Every variable is in the form of data member name and parameter  from the main function.
Member Intitializer in C++
Member Intitializer

Example of Member Initializer and Static Variable

#include<iostream.h>
class Bank                        // Class Name
{
    private:                  
        const int password;        // Constant Integer
        char *name;                // Pointer Character For Dynamic Charracter Array
        int amount;                // Simple integer
    public:
        static int totalObject; // Static Integer
        Bank(int pass):password(pass), name(NULL),amount(0) // Member Intilizer For Constant Variable
        {
            totalObject++; // When Constructor will call it will one to variable
            name= new char [20];  // Assigning memory to name
        }
        // Setters.....
        void setName(char *n)    // setter for Name
        {
            name=n;
        }
        void setAmount(int am)  // Setter For Amount
        {
            amount=am;
        }
        // Getters
        char* getName()
        {
            return name;
        }
        int getAmount()
        {
            return amount;
        }
        const int getPassword()
        {
            return password;
        }
        // Function For Transaction
        void Deposit(int DA) // Function To Deposit Amount
        {
            amount=amount+DA;
            cout<<"After you Deposit amount your new balance is:"<<amount<<endl;
        }
        void withDraw(int wd)  // Function to Withdraw and Update Amount
        {
            amount=amount-wd;        // Amount will Here
            cout<<"After Withdraw Your New Amount is:"<<amount<<endl;
        }
      
};
// Intiliazing Static variable
int Bank::totalObject=0;

// Main Function
void main ()
{
    int deposit,witdraw,password,amount;
    char name[20];
    // Entering Data For Account
    cout<<"Enter User Name: ";
    cin>>name;
    cout<<"Enter Pssword For Account: ";
    cin>>password;
    Bank Account(password); // Creating Object and assigning constant value
    Account.setName(name);
    cout<<"Enter Amount For Your ACCOUNT:";
    cin>>amount;
    Account.setAmount(amount);
    cout<<"Enter Amount To Deposit:";
    cin>>deposit;
    Account.Deposit(deposit);
    cout<<"Enter Amount To Widraw:";
    cin>>witdraw;
    Account.withDraw(witdraw);
    // Displaying Account Information
    cout<<"Total Objects In Class::"<<Bank::totalObject<<endl;
    cout<<"****Your Complete Account Information*****"<<endl;
    cout<<"Account Name        ::"<<Account.getName()<<endl;
    cout<<"Account Password  ::"<<Account.getPassword()<<endl;
    cout<<"Amount In Account ::"<<Account.getAmount()<<endl;

}

Output of program

 
C++ Project Foe Beginner
Bank Account

Sunday, 26 October 2014

Interview Question For Association, Composition and Aggregation in OOP

Explanation of  Association, Composition and Aggregation in OOP with examples (C++)

Association

C++ Implementation of Association
It is the relationship between two or more objects, where every objects have its own life cycle and there is no owner of these objects. Let's have an example of association between student and tutor. One student can teach by one tutor, many student can teach by one tutor or many tutor can associate with a single student and also many student associate with many teacher or tutor. Here student and tutor are the objects and both have no owner. They can build and delete independently. Association is represented by a solid line.

Generalization

In object oriented modal some classes may have common characteristic we extract there common feature into new class and inherit original classes from this new class. For example we have two classes Teacher with characteristic name, age, sex, birthday, class, salary and job title  Student with characteristic name, age, sex, birthday, class, fee and GPA. Both classes have some common characteristics so we extract these feature in new class called Person.
Generalization

Specialization 

If we want to add new class to a existing modal, find the similar characteristic into model but some part of behavior of new class is different are restricted for that we can use specialization. For example in previous table we had Person as parent class and teacher and student as child class now we want to add adult class with same characteristics but here age should be more then 18.
Specialization

Class Association

Class association is implemented in term of inheritance implemented generalization or specialization relationship between the classes in case of public inheritance it implement "Is A" relationship and in private inheritance "Implemented in term of" relationship. Member of base class are available to child class.

Object Association

Its interaction of one class object with a objects of other classes.

Aggregation

It is specialize form of association. In this an object may contain collection of other objects, relationship between container and contained object is known as aggregation. Here every objects have their own life cycle but they are owned by other class and these objects can't be owned by any other class. Lets take an example of  Room, here room is parent class and its child classes are chair, table, bad and mirror. If any of child class is removed it will not affect other class. It is represented by blank diamond.
Some important point of aggregation
  • Relationship between objects are "Has A"
  • Every Object have independent life
  • Child Parent relationship

Commposition

It is specialize form of Aggregation. In this an object maybe compose of other smaller objects. Its the relation of "Part and Whole". It also known as Has A  relationship and it is represented by filled diamond. In this every child class does not have life cycle they are dependant on parent class. If parent class is deleted all child class will also deleted. Lets have an example, here Human body and body parts have relationship.
Some important points of commpositon
  • Strong Relationship
  • Only Parent have independent life cycle
  • Parent child relationship

Saturday, 18 October 2014

Object Oriented Programming Task For Time Clock In C++

OOP Time Clock Parametrized and Default Constructor

Task To Convert Twenty Four Hour Format To Twelve Hour Format

Complete the function definitions of the following ADT
class Time
{
public:
void setTime( int, int, int ); // set hour, minute, second
void setHour( int ); // set hour
void setMinute( int ); // set minute
void setSecond( int ); // set second
int getHour(); // return hour
int getMinute(); // return minute
int getSecond(); // return second
void printTwentyFourHourFormat(); // print universal time

void printTwelveHourFormat(); // print standard time 

private:
int hour; // 0 - 23 (24-hour clock format)
int minute; // 0 - 59
int second; // 0 - 59
};


Task Solution

#include<iostream.h>
#include<iomanip.h>
class Time
    {
        public:
            void setTime( int hour, int minute, int second ) // set hour, minute, second
            {
                setHour(hour);
                setMinute(minute);
                setSecond(second);
            }
            void setHour( int h ) // set hour
            {
                hour=(h>=0 && h<24)? h:0;
            }
            void setMinute( int m ) // set minute
            {
                minute=(m>=0 && m<60)? m:0;
            }
            void setSecond( int s ) // set second
            {
                second=(s>=0 && s<60)? s:0;
            }
            int getHour() // return hour
            {
                return hour;
            }
            int getMinute() // return minute
            {
                return minute;
            }
            int getSecond() // return second
            {
                return second;
            }
            void printTwentyFourHourFormat() // print universal time
            {
                cout << setfill( '0' ) << setw( 2 ) << hour << ":" << setw( 2 ) << minute << ":" << setw( 2 ) << second;
            }
            void printTwelveHourFormat() // print standard time
            {
                cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 )
                << ":" << setfill( '0' ) << setw( 2 ) << minute
                << ":" << setw( 2 ) << second << ( hour < 12 ? " AM" : " PM" );
            }
            //Void incSec(int=1); // increment in the second (01)
            //Void incMin(int=1); // increment in the minute (01)
            //Void incHour(int=1); // increment in the hour (01)
        private:
            int hour; // 0 - 23 (24-hour clock format)
            int minute; // 0 - 59
            int second; // 0 - 59
    };

void main ()
{
    Time t;
    t.setTime(21,4,55);
    t.printTwelveHourFormat();
    cout<<endl;
    t.printTwentyFourHourFormat();
}

E-mail Newsletter

Sign up now to receive breaking news and to hear what's new with us.

Recent Articles

TOP