Data Structure and Algorithms based on the Object-oriented programming (OOP) Concepts such as Classes, Objects, constructor, Methods or member functions (later will explain their differences in next Lab Manual), inheritance, polymorphism etc. Having good command on these basic concepts will help you to understands DSA concepts and Algorithms better because we will have to code every single Data structure & Algorithm using the Concepts of OOP using C++. That is why I have designed this manual according to the requirement of DSA lab to perform future tasks without any problem.
Prerequisite of this Lab is all those students who have basic concepts on C++ Programming such as Syntax of Language, concept of functions, pointers, datatypes etc. Visual Studio 2012 and onward version will be use for implementation and programming tasks assigned in the Lab.
Peepgrams.com

Today’s Concept:
Classes & Objects
Will also cover the Subtopics:
Constructor & Attributes
Class is the Building block in C++ language which works on the principles of Object Oriented Programming its just like a blueprint for an object which holds its own data members (Attributes) and member Functions (Method), and they can be accessed by creating and using the instant (object) of the class without creating object you can never use the class properties. Class is user-defined in which user can have all data members and member function of his/her choice this makes the classes more important concept in OOP. Major difference between classes and Structure is Classes by default private in Class when they created but in Struct, they are public by default when they created.


Syntax Example:

class Name of Class //naming of class will be same as naming of variable
{
       Access specifier: //such as public, private, protected
    Data members/properties; //have any datatypes variables or array of any datatype
       Data members()//called method or function
       {
       } 
};

An Objects is an instant of a Class. Object is used to access the class properties such as member functions and data members. Object also has very importance because when class is created no memory is allocated to class (Attributes & functions) but when object of that class created (instantiated) memory is allocated.
Syntax Example:
int main()
{
       class_name  obj_name; //class name is the name of class and object naming
                                                //will be same as naming of variable
   return 0;
}
Attributes are the data members of the class which also called properties of the class. Attributes can be of any type for instant char, int, float, double, string etc. or array of any type.
Constructor is a member function of the class which use to instantiate the object that means when object create constructor of the class automatically called. Constructor is special type of the function which has the same name as the Class, Constructor don’t have the return type and the one most important thing about the constructor that if the user do not create it compiler construct it by default (which doesn’t have any parameter and body)
There are 3 types of constructor in OOP (C++):
  1. Default constructor (not parameters and empty body)
  2. Parameterised constructor (which have parameters and body too (can be empty))
  3. Copy Constructor (which use to copy the two same objects)

Syntax Example:
 #include<iostream>
using namespace std;
class DSA_Lab
{

    public:
    int id;
     
    //Default Constructor
    DSA_Lab()
    {
        cout << "Default Constructor called" << endl; 
        id=-1;
    }
     
    //Parametrized Constructor
   DSA_Lab(int x)
    {
        cout << "Parametrized Constructor called" << endl;
        id=x;
    }
   DSA_Lab (DSA_Lab &){
            cout << "Copy Constructor called" << endl;
   }
};
int main() {
     
    // obj1 will call Default Constructor
   DSA_Lab obj1;
    cout << "DSA_Lab id is: " <<obj1.id << endl;
     
    // obj1 will call Parametrized Constructor
    DSA_Lab obj2(21);
    cout << "DSA_Lab is: " <<obj2.id << endl;
    return 0;
}

Lab Activity Tasks:
  1. Create a C++ program to create a Class Person which have the following attributes name, address, id etc. also create the main (test class) function to create the object of that class.
  2. Create a C++ program to Create class of Car named as Cultus choice the data members (attributes) by yourself which you think suits it or must have it also create default constructor for the class.
  3. Create C++ program to create a class name Rana which have the following attributes such as name (strings), age (int), and gender (char) just initialize all the data members using parameterized constructor.
  4. Create C++ program to create class name Dog which have the properties (attributes) such as name, age etc. now after that create two objects of the same class assign following information to the data members of first obj as Alex, 2 and copy the value of same to the second object using copy constructor.


Bonus Marks you have to decide what type of concept you have to implement on below Tasks
  1. Create a C++ program to create a class name Students which have name, id and class section such as A,B,C ect. Take at least 10 students record using main class.
  2. Create a C++ program which hold information of hotel you have to provide all information when you are creating the object and print the desire information on the console.














Post a Comment

Previous Post Next Post