# Posts by: bhishan

I am Bhishan Bhandari, a CS student and life hacker. I specialize in automation. I sell my services on fiverr. You can hire me for projects here Buy Services Follow me on github for code updates Github You can always communicate your thoughts/wishes/questions to me at bbhishan@gmail.com

## Implementing Stack using List in Python – Python Programming Essentials

- - Web

#### Intro

Stack is a collection of objects inserted and removed in a last-in first-out fashion (LIFO). Objects can be inserted onto stack at any time but only the object inserted last can be accessed or removed which coins the object to be top of the stack.

#### Realization of Stack Operations using List

Methods Realization using List Running Time
S.push(e) L.append(e) O(1)*
S.pop() L.pop() O(1)*
S.top() L[-1] O(1)
S.isempty() len(L) == 0 O(1)
len(S) len(L) O(1)

#### What is O(1)* ?

The running time for push and pop operations are given O(1)* in the above table. This is known as amortization. It is a principle used in complexity analysis of data structures and algorithms. It should be used carefully and for special cases only.

##### Why did we use amortized analysis for push/pop?

The list(our stack’s underlying data structure) is a series of objects which eventually are realized by arrays. The objects are stored in a continuous block of memory which offers indexing property for lists. As such, a list cannot occupy the entire memory but restricts to some specific size. When there is no more space for the objects to be added to the end of the list, a new memory series is allocated with the increased size, all the objects are copied to the new allocation and new object is added next to the last object of the current series. The previously held memory is then released free. Here, on every append, resizing of list is not required but true once in a while. Hence the running time of append in list (push on stack) for most elements is O(1) but as a whole in an amortized sense, it is O(1)* which accounts for the timely resizing and copying of elements.

Similarly for pop operations, shrinking of the underlying list is done once in a while therefore accounting for an amortized complexity of O(1)*

#### Implementation of Stack using List

```class ListStack:
def __init__(self):
self._data = []

def __len__(self):
return len(self._data)

def isempty(self):
return len(self._data) == 0

def top(self):
return self._data[-1]

def push(self, e):
self._data.append(e)

def pop(self):
return self._data.pop()
```

#### Conclusion

Stack is an important data structure for realizing solutions to various programming problems. As such, it is even more essential to understand the running time evaluations and working mechanism of these data structures.

Hire me for a project Fiverr

## Raising and Handling Exceptions in Python – Python Programming Essentials

- - Tutorials

#### Brief Introduction

Any unexpected events that occur during the execution of a program is known to be an exception. Like everything, exceptions are also objects in python that is either an instance of Exception class or an instance of underlying class derived from the base class Exception. Exceptions may occur due to logical errors in the program, running out of memory, etc..

#### Common Exception Types

Class Description
Exception A base class for most error types
AttributeError Raised by syntax obj.foo, if obj has no member named foo
EOFError Raised if “end of file” reached for console or file input
IOError Raised upon failure of I/O operation (e.g., opening file)
IndexError Raised if index to sequence is out of bounds
KeyError Raised if nonexistent key requested for set or dictionary
KeyboardInterrupt Raised if user types ctrl-C while program is executing
NameError Raised if nonexistent identifier used
StopIteration Raised by next(iterator) if no element
TypeError Raised when wrong type of parameter is sent to a function
ValueError Raised when parameter has invalid value (e.g., sqrt(−5))
ZeroDivisionError Raised when any division operator used with 0 as divisor
##### For an example, following produces a TypeError exception
```abs(‘hello world’) #expects numeric parameter but string given
```
###### Example of ValueError

Although the type of the passed parameter is correct, the value is illegitimate.

```int(‘hello world’)
int(‘3.14’)
```

#### Raising an Exception

An exception can be raised from anywhere within the program though the keyword raise followed by an instance of any of the exception classes.

For example, when your program is expecting a positive integer to process but the I/O stream sent a negative integer, you could raise an Exception as such:

raise ValueError(‘Expecting a positive integer, got negative’) #instance of ValueError exception class

#### Handling an Exception

Now that we have talked on raising an exception, we should program such that the exception is dealt as required, else the execution of the program terminates. It is advisible to catch each exception types separately although python allows a more generic exception handling for any type of exceptions that may occur.

Examples of Common Usage:

```try:
result = x/y
except ZeroDivisionError:
#do as per required
```

Other common exception handling:

```try:
fp = open(‘sample.txt’ )
except IOError as e:
print( Unable to open the file: , e)
```

#### Conclusion

Exceptions are an important principles of programming for any languages. It should be used wisely. On a concluding note, a try-except block can have a finally block as well. An example of use of finally can be to close a connection regardless of the successful or failed transmission of messages. Additionally, a try-except combination can have a single try block with multiple except blocks catching various classes of exception.

Hire me for a project https://fiverr.com/bhishan

## Automation With Python Python Codes To Create Dropbox Apps

- - Uncategorized

As promised in the article earlier on Automate DropBox Signups using python, I have come up with an article along with the codes to create an app and fetch the API keys for it which then allows us to access the files in dropbox. Well, again we stick to selenium module for an ease. In the last article, I’ve explained a python script to automate the signups for dropbox. Now that we have enough cloud space in different accounts. We now need to access the files in those spaces so we can use it as a file server. DropBox provides a feature to create apps on dropbox and gives API keys to hence access the files in the account. Since we’ve got multiple dropbox accounts we would stick towards automating the procedure to get the api key for accessing the files.

```from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
list_of_inputs = browser.find_elements_by_xpath("//div/input[starts-with(@id,'pyxl')]")
list_of_inputs[0].send_keys("email@domain.com")
sign_in[len(sign_in)-1].click()
time.sleep(10)
browser.get("https://dropbox.com/developers/apps/create")
time.sleep(3)
type_of_app = browser.find_elements_by_xpath("//*[contains(text(),'Dropbox API app')]")
type_of_app[0].click()
file_access[0].click()
type_of_file_access[0].click()
app_name = browser.find_element_by_name("name")
app_name.send_keys("appnamewhichisuniquelolo")
create_app = browser.find_elements_by_xpath("//*[contains(text(),'Create app')]")
create_app[1].click()
time.sleep(7)
app_key_item = browser.find_element_by_class_name("app-key")
app_key = str(app_key_item.get_attribute('innerHTML'))
app_secret_item = browser.find_element_by_class_name("app-secret")
app_secret = app_secret_item.get_attribute('data-app-secret')
print app_key, app_secret

```

General Idea of Automation

The general idea for automation is to mimic the manual workflow and put it in a loop or assign a cron job(it’s kind of same thing but not really). For creating apps on dropbox, I did the same thing. The codes are self-explanatory. We’ve used selenium and time module throughout our program. We use selenium for initiating as well as interacting with the browser. You can see, we’ve used time.time(time_in_seconds) method from time module. Depending on the speed of the internet, we need to set this up. Failing to do so will lead the program to misbehave since it will start looking for some element even when the page hasn’t been completely loaded. We fuel our program with the varieties of methods selenium provides. The above codes however shows only the procedure to create an app for a single account and print the api keys. You should loop over some file containing email id’s and password and save the api keys to some file in real usage. Hint: Place a loop over the codes and once done with getting api keys, logout from the current account.

Do comment below how you felt the article was. Any queries, please mention below.

Announcement

## Google Search Using Selenium And Python Selenium Python Basics

- - Uncategorized

After a busy week at college and internship, finally I get free time at weekend to write my first article for August 2015. We discuss about some common methods of selenium module in python today. Selenium is a library used for automated browser testing. However, in this post we discuss about using selenium module in python to make a google search. The post breaks down into various blocks explaining on how to open a url in the browser via selenium python, search presence of a url in a page, click links present in a page and also open a new tab. These are the necessities to get started with selenium. You may also like to read my article on how to login to a website using selenium python. Starting quickly with no further delay.

Necessities to begin

1. python installed

2. selenium module installed

For Linux:

sudo pip install selenium

For Windows:

pip install selenium

```from selenium import webdriver
from selenium.webdriver.comon.keys import Keys
q = raw_input("Enter the search query")
q = q.replace(' ', '')
browser = webdriver.Firefox()
body = browser.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
counter = 0
for i in range(0,20):
browser.get("https://www.google.com/search?q=" + q + "&start=" + str(counter))
body = browser.find_element_by_tag_name("body")
if "thetaranights" in body.text:
browser.find_element_by_xpath('//a[starts-with(@href,"http://www.thetaranights.com")]').click()
break
counter += 10
```

### 1. Import statements (Line 1 and 2)

These are the import statements that are required for initiating a browser later in our program and passing url parameters to the address bar in the browser.

### 2. Get query for google search (Line 3 and 4)

Here, we are taking a query for the google search via raw_input. Here is an example url for a google search which requires the spaces between the words to be replaced by “+” , an additional parameter start=0 is seen which specifies the search result of page 1. Similarly start=10 gives the search result of page 2.

Hence, we after taking the input from the user, we replaces the spaces with +.

### 3. Instantiate a browser (Line 5)

The statement browser = webdriver.Firefox() opens up a new browser window.

### 4. Opening a new tab (Line 6 and 7)

These statement opens a new tab. The statement body = browser.find_element_by_tag_name(“body”) is to make sure we actually inside current tab’s body so that we can open a new tab with the combination of keyboard. body.send_keys(Keys.CONTROL + ‘t’) will open a new tab. For Mac replaceing CONTROL with COMMAND should work.

### 5. Opening a url in the browser (Line 10)

For opening a url in the browser, all you need to do is pass the url as an argument to the browser.get method. Remember I’ve given browser.get because we instantiated the browser earlier with browser = webdriver.Firefox().

### 6. Searching for a presence of certain url/text in the search result (Line 11 to 15)

Now again we assign the body of the current tab to the variable body. Then we check if “thetaranights.com” is present in the search result. If present, we run the statement browser.find_element_by_xpath(‘//a[starts-with(@href, “http://www.thetaranights.com”]’).click to search for the url in the search result which starts with “http://www.thetaranights.com” at the beginning and anything after it. We then use .click() over it to open the url. Since the result we are looking for is found and clicked. We break the loop. If the earlier statement if “thetaranights.com” in body.text was false meaning not found we would iterate and search for another page of google results and so on until 20 pages.

Note: You can close the webbrowser with browser.quit()

So, now we know how to open a browser, open a new tab in the browser, go to certain website/url, search for link in the body of the page and click the link. If you have any questions regarding the codes/article, please mention below in the comment section. You may also be interested in my article on How to login to a website using selenium. Happy Coding

## How To Split And Merge Pdf Documents

- - Uncategorized

Not the type of posts I usually produce. A promotional review of a tool.

Everyone knows that PDF files are hard to work with. Apart from figuring out how to convert PDF documents, oftentimes we’re also trying to put together the best PDF document possible from other content.

But when those content sources are already in the PDF format, it can seem like an uphill battle just to get the content separated. More often than not, we need to figure out how to manipulate PDF documents at the page level.

Sometimes we may need to rework a PDF document by adding or removing a few pages. Manipulating PDF documents like this can seem intimidating at first.

If you have legal PDF documents your concern may be preserving the integrity of the PDF pages, or if you’re working with reports, you may be worried about deleting the original PDF pages for good.

Normally, you’d have to convert the entire PDF file into a Microsoft Word document, delete or insert the pages accordingly, and then convert it back to PDF. But there’s an easier way to do it.

With a tool like Able2Extract 10 from Investintech.com, you can merge and split your PDFs as easily as you can select a page. This latest version comes with features for converting, creating and editing PDF documents.

Under the latter category, Able2Extract 10 has added the ability to merge and split PDF files. It does this by letting you extract or insert PDF pages to your currently opened PDF document.

For instance, if you have blank pages or full page images in a PDF you’d like to remove or collect into one file, you can extract them into a completely separate file. Or, if you’d like to add some supplementary information to compliment your existing PDF content, you can add them page by page into an existing PDF document easily.

Here’s a look at how this can be done with Able2Extract 10’s latest PDF splitting and merging feature.

To Merge PDFs:

1. Open the PDF you wish to add pages to in Able2Extract 10.

2. Click on Edit from the toolbar

3. From the side editing panel, select Insert From PDF

4. From the dialog that appears, select your PDF file from which you want to insert pages from. Click on Open.

- - Uncategorized

This short post will walk through simple steps to download udemy videos which are not download-able from the website. Most of the paid udemy courses as well as some free courses are unavailable to download at udemy.com . I personally have around 200 courses in my account. Now most of these courses were not available for download. Fortunately I found a python script on the internet which solved my problem easily.

I introduce you all to udemydl which is a python script.

1. I assume you have python installed in your device. If you are running on a Linux environment then python is pre-installed.

2. If you don’t have pip installer for installing python packages, have it installed via following command.

sudo apt-get install pip

3. Now that you have pip installed type the following command the python script that enables the download of udemy courses.

sudo pip install udemy-dl

4. Below is the command to download the udemy course. You will need the url of the course.

udemy-dl https://www.udemy.com/COURSE_NAME

Alternatively you can use the following command to download the course by passing username and password at the same time as parameters. Below is the command.

``````Thanks

## Website Mobile Friendly Tester Automation Script Python Codes For Mobile Friendly Test

- - Uncategorized

Hey Guys, I am back again with another script that may pronounce useful to website owners, search engine optimization experts as well as normal people like me. Through the codes we write and discuss in this article, you will be able to check if a website is mobile friendly or not. Well, here I offer a bonus. Through the codes you will be able to issue a number of websites for a mobile friendly test instantly at a time. Why is it necessary? Here’s the answer. As of the latest update in google’s search algorithm, the search engine lord now considers mobile friendliness as a major ranking factor for a website.

# Python script to automate mobile friendly test

Before we begin

Before we begin our coding, let me make few things clear. We will be writing 2 files although one will be a simple text file and another will be a python file. In this text file we will write the names of the domain we want to issue for a mobile friendly test, one in each line in the format domain.com i.e without www

```from json import loads
import mechanize
br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]

with open('websitesformobilefriendlytest.txt') as f:
for line in f:
if json_obj["ruleGroups"]["USABILITY"]["pass"] == True:
print "Congrats " + str(line)  + " is mobile friendly"
else:
print str(line) + " is not mobile friendly"

```

1. Line 1 and 2

These are the import statements as we will be using mechanize module to query the mobile friendly test via a browser instantiated by the module and the response is a JSON hence we import loads from json.

2. Line 3 to 5

On line 3 we use the Browser() method of mechanize to instantiate a browser. Line 4 is a statement that tells to ignore the robots.txt file. On line 5, we specify a user agent.

3. Line 7 to 14

Line 7 opens the text file where we previously stored the names of the domain. We now can reference the content of the file via variable f.

Line 8 is the start of the for loop which stores the name of the domain in the variable line on each iteration.

On line 9, we query a domain name/ website for a mobile friendly test. The specified url will return a response of the test result which we store in a variable google_results

On line 10, we read the response and load it as a json object to a variable json_obj.

Now on line 11, we have a conditional statement to check if the website passed the mobile friendly test. The test result is a boolean value which is a value for the key “pass” which is again a value for the key “USABILITY” which in turn is a value for the key “ruleGroups” in the json_obj. Below is the example of how it may look.

{“ruleGroups” : {“USABILITY” : {“pass” : Ture/False}}}

If the website passed the mobile friendly test, the value will be True else False. Based on the result, we then print whether a website is mobile compatible or not.

Well, here is the bonus code. Let me know if you have any questions regarding the codes in the comment section below. Also, here’s a similar program (Is it a wordpress website checker script)with explanation on the codes which can help you understand and implement these codes. Thanks for reading

```from json import loads
import mechanize
import datetime
rowdict = {}
rowdict['date'] = str(datetime.date.today())
worksheet_id = 'od6'
client.debug = True
client.email = 'email@domain.com'
client.source = 'mobilefriendlytest'

br = mechanize.Browser()
br.set_handle_robots(False)
br.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]

with open('websitesformobilefriendlytest.txt') as f:
for line in f:
rowdict['website'] = str(line)
if json_obj["ruleGroups"]["USABILITY"]["pass"] == True:
#print "Congrats " + str(line) + " is mobile friendly"
rowdict['ismobilefriendly'] = "yes"
else:
#print str(line) + " is not mobile friendly"
rowdict['ismobilefriendly'] = "no"
```

## Hadoop Starter Kit What Is Big Data

- - Uncategorized

I just watched a 18 minutes video on introduction to Big Data & Hadoop on Udemy. Here’s a link https://www.udemy.com/hadoopstarterkit/learn/ to the course I’ve enrolled in if you’d like too. I would like to brief what I learned.

# What is Big Data?

There are mainly three factors that very well helps define a big data. Volume, Velocity and Variety.

Let me take an example of an imaginary startup company who has around 1 TB of data at the initial phase. How do we define the data? Does it qualify for a big data? Well if I say the amount of data is going to be stable throughout the lifetime of the company, is it a big data? Certainly not. For a data set to be called big data, it should have a good growth rate thereby increasing the volume of the data and should be of different variety (text, picture, pdf, etc).

Here are some of the examples of big data.

Companies like Amazon monitors not only your purchase history and wishlist but also each clicks, recording all the pattern and processing this big amount of data thereby giving us a better recommendation system.

Here’s what NASA has to say about big data.

In the time it took you to read this sentence, NASA gathered approximately 1.73 gigabytes of data from our nearly 100 currently active missions! We do this every hour, every day, every year – and the collection rate is growing exponentially. – See more at: http://open.nasa.gov/blog/2012/10/04/what-is-nasa-doing-with-big-data-today/

Have a look at this

# Big Data Challenges

Storage – Storage of data should be as efficient as possible both in terms of hardware and processing and retriving the data.

Computation Efficiency – It should be suitable for computation

Data Loss – Data may be lost due to hardware failure and other reasons. Hence data recovery strategies must be good.

Time – Big data is basically for analysis and processing, hence the amount of time for processing the data set should be minimal.

Cost – It should provide huge space and should also be cost effective.

RDBMS

The main issue is scalability. Once the data increases, the amount of time for data processing goes higher with unmanagable number of tables forcing us to denormalize. Necessities may arise to change the query for efficiency. Also RDBMS is for structured data set only. Once the data is present in various formats, RDBMS cannot be used.

GRID Computing

Grid computing creates nodes hence is good for compute intensive. However, it does not perform well for big set of data. It requires programming in lower level like C.

Supports huge volume

Storage Efficiency both in terms of hardware and processing/retrival

Good Data Recovery

Horizontal Scaling – Processing time is minimal

Cost Effective

Easy to Programmers and Non Programmers.

So is Hadoop going to replace RDBMS? No. Hadoop is one thing and RDBMS is another better for specific purposes.

Storage : Perabytes

Horizontal Scaling

Cost Effective

Made of commodity computers. These are cost effective but enterprise level hardware.

Batch Processing System

Dynamic Schema (Different formats of files)

RDBMS

Storage: Gigabytes

Scaling limitted

Cost may increase violently with volume

Static Schema

## Mailchimp Subscription Integrating WordPress And Mailchimp

- - Uncategorized

# Integrating Mailchimp and WordPress for Better Results

Mailchimp is an email marketing company which is free for upto 2000 subscribers.

Entrepreneur : Send 12,000 emails to 2,000 subscribers for free. No credit card required while sign up. Mailchimp promises this package to be free forever.

Growing Business : 50,000 subscribers , unlimited number of emails.

High volume sender : More than 50,000 subscribers

Though we will stick to the Entrepreneur plan for this post.

Why do you need Mailchimp ?

When you have a satisfactory traffic to your website/blog, and want the flow to remain and increase, it’s best to shuttle your blog with a subscribe button. But you need to make sure you provide quality content to your subscribers.

The need for mailchimp arises when you have pretty good quantity of followers and need a medium to disseminate your news/blog post to the audiences.

1. Visit Mailchimp. When on homepage, have a look at the upper right of the page and click on Sign Up Free.

2. Enter your details when prompted. It is recommended to use your website’s official mailing id but alternatively you are free to use any valid email. Enter other details like username and password and finally stress the Create My Account button.

3. Before starting off with MailChimp, you should click the conformation link in your email. On the landing page, you need to verify your humanity (Captcha). As soon as you are done with verification, you will now have to enter informations for your account.

These include your name, company/website information, address, etc. Make sure you fill up each fields before submitting the form.

Once done you will be prompted to a page simillar to this.

4. Moving on, click on Creat A List as shown in the figure. Since we have no lists, we are going to create one. Click on Create List button on the right top of the screen.

You will now to taken to a form. The list name must be sensible as it can be seen by your subscribers. “Your Website Name Newsletter” may be an appropriate name for the list.

Put the email address associated with your blog/website and an appropriate name (website name or your own name).

Remind people how they got on your list section is a way of telling people how they are incorporated with your website or a gentle reminder about their role in your website. For Example : “You received this email because you have subscribed to Your Website Name Newsletter”.

Enter other essential details and submit the form.

5. Now that you have successfully created a mailing list, it’s time to get people subscribe to your newsletter. Let’s get to our wordpress powered website/blog to get things done.

Under plugins, go to add new and search for “mailchimp widget”.

Install and activate the plugin. As soon as you do so, you get a message on the top of the dashboard similar to one below.

Click the link and you will be prompted for an API key.

6. Login to your mailchimp account and click account as shown below.

Next up, click on extras and then on API keys. Scroll down until you see Create A Key option. Click on it to get the API key.

Now copy the API key and paste it in the field asking API key in your wordpress blog and save the changes.

7. Now go to Widgets which is under Appearances and drag the MailChimp List Signup widget to the sidebar your theme allows. I am using Noteworthy theme at the moment, therefore I have right sidebar where I am going to place the MailChimp List Signup widget.

Since we have only one mailing list so the widget selects the available mailing list by default. Change the title to something appropriate and changing button text to “Subcribe” will be better. Click on Collect First Name. You may edit the success and faliure message as well.

All set and done. Just wait for the subscribers and grow your business. You can view the subscribers in your mailchimp account under the mailing list you created. To throw an email to everyone in the mailing list, create a campaign and that’s all it takes. Take some time to comment on my writing.

## Object Oriented Programming With C Constructors Getter Setter

- - Uncategorized

Object Oriented Programming C++

Here through this article, we will discuss about the basics of Object Oriented Programming. Our codes will be based on C++ programming language while the concept is the same for other OOP languages too. We will write 3 files amongst which one is the header file, the second one is the implementation of the header template. Finally we will have one main program. By the end of this read, you will be able to write codes in Object Oriented Programming languages. We will cover constructor, destructor, setter and getters.

Class definition file Computer.h

```#include
using namespace std;

class Computer{
private:
string deviceType;
string nameofBrand;
public:
Computer(string brandName="lenovo",string typeofDevice="laptop");
~Computer();
void setBrandName(string brandName);
void setDeviceType(string typeofDevice);
string getBrandName();
string getDeviceType();
void displayDeviceInfo();
};

```

The above program shows the structure of our class Computer. The file Computer.h is our class template file.

1. Line 1 and 2 are the include statements of our input/output header file i.e iostream

2. Line 4: Our class for this example is Computer in which the starting alphabet is capital which is the convention of OOP.

3. Line 5 to 7: In C++ we place the private variables after the keyword private followed by a colon. For our example we have two private variables deviceType and nameofBrand. The private variables cannot be accessed by the object.variableName while it is possible to access it via member functions i.e the methods that are public. Basically on a general sense, private variables can be accessed only within the class.

4. Line 8 to 15 are the member functions of class Computer. Here the functions/methods are placed after the keyword public: . This means the object of class Computer can access these member functions directly via object.memberFunction().

5. Line 9 and 10 are different than the other member functions. Line 9 is the definition of the constructor for our class Computer. The constructor contains the name same to the class name. This is the convention for all the OOP. A constructor has no return type as it is basically used for the initialization of the private variables. The code inside the constructor runs at the time of object creation. In our header file, we have two parameters in the constructor and each of the parameter is initialized by the default value. Line 10 is the definition of destructor. In C++ destructor has same name as the class name except it contains “~” sign before the name. Destructor are basically used to destroy other classes initialized in the current class.

6. Line 11 to 14 are the setter and getter methods for the private variables deviceType and nameofBrand. The setter methods have no return type and takes values through parameters which are to be set to the private variables. The getter methods are used to access the private variables and takes no parameter as it’s function is to return the value and not accept any parameters. Therefore getters have return type which is based on the type of private variables.

7. Line 15 is the member function like all others which has return type void and takes no parameter/argument.

The following file is Computer.cpp file which contains the implementation of the class definition Computer.h

Class implementation file Computer.cpp

```#include
#include "Computer.h"
using namespace std;

Computer::Computer(string brandName,string typeofDevice){
setBrandName(brandName);
setDeviceType(typeofDevice);
}

Computer::~Computer(){
cout<<"Object Destroyed!!"<<endl;
}

void Computer::setBrandName(string brandName){
nameofBrand = brandName;
}

void Computer::setDeviceType(string typeofDevice){
deviceType = typeofDevice;
}

string Computer::getBrandName(){
return nameofBrand;
}

string Computer::getDeviceType(){
return deviceType;
}

void Computer::displayDeviceInfo(){
cout<< "It is a   " << getDeviceType() << "and belongs to  "<< getBrandName()<<endl;
}

```

1. Line 1 to 3 contains the include statements. We have to include the header file Computer.h in our implementation file. The standard header files are included via statement #include<header> while the header files created by the user are included via statement #include “Header.h”

2. Line 5 to 8 is the implementation of the constructor of the class Computer. It takes two arguments namely brandName and typeofDevice. Inside the function setBrandName and setDeviceType methods are called with the parameters brandName and typeofDevice respectively. Whenever an object of class Computer is created, the codes inside the constructor is run immediately.

3. Line 10 to 12 is the implementation of the Destructor of the class Computer. The destructor is basically used to terminate/kill the objects of the other classes initialized in the current class. In our example, we have done nothing but printed that the object has been destroyed.

4. Line 14 to 16 is the implementation of the method setBrandName. It is a setter method. Conventionally setter method begins with “set” followed by the variable name. Our setBrandName takes one argument and is of return type void. Inside the method, nameofBrand is set to the value passed in as an argument. nameofBrand is our private variable hence a public method is used to access and alter it’s value i.e setBrandName.

5. Line 18 to 20 is the implementation of the setDeviceType. Similar to the setBrandName method, it is also a setter method. This method is used to set the value of the private variable typeofDevice. This method also takes one argument and is of return type void.

6. Line 22 to 24 is the implementation of the method getBrandName. Unlike setBrandName, getBrandName is a getter method that is used to return the value of a private variable which in this case is nameofBrand. The return type of a getter method is same as the type of variable it returns. In our example, getBrandName is of string return type which takes no parameter/argument.

7. Line 26 to 28 is also a getter method that is used to return the value of the variable deviceType. It is of string return type because it is used to access the value of the variable deviceType which is of type string.

8. Finally we have our last method in the class computer which in this case, we are using to print out the information of the device based on the entries entered at the type of object creation. Method displayDeviceInfo is a void return type method that takes no parameter. Here we are using the standard of method of accessing the private variables i.e using getter methods. The method when invoked on an object prints the deviceType and nameofBrand.

Let us take a look at our main program where we create objects of class Computer and invoke various methods of the class. Below is the main program.

Main program testprogram.cpp

```#include
#include "Computer.h"
using namespace std;

int main(){
string deviceBrand;
string typeofDevice;

Computer computers[5];

for(int i = 0; i < 5; i++){

cout<< "Enter the brand of your computer for position "<< i+1<<endl;
getline(cin,deviceBrand);

cout<< "Enter the type of computer for position "<< i+1<<endl;
getline(cin,typeofDevice);

Computer objectHolder(deviceBrand, typeofDevice);

computers[i] = objectHolder;
}

for(int i = 0; i < 5; i++){

Computer objectHolder = computers[i];
objectHolder.displayDeviceInfo();
//computers[i].displayDeviceInfo();
}
}

```

1. Line 1 to 3 are the statements to include the iostream and our Computer class that we coded earlier. As discussed earlier, we include the non-standard class (Computer.h in this case) in the format #include “Header.h”. One thing to note is that we include the class definition file and not the implementation file.

2. Line 6 to 7, we declare two variables of type string.

3. Line 9 begins the OOP portion. Here we are declaring an array of type Computer of size 5. This means each index of the array computers can hold an object of Computer class.

4. Line 11 to 23 is a for loop where we iterate for the number of times equal to the size of our array I.e five. We then take input from the user for the variables deviceBrand and typeofDevice declared earlier. Next, we create an object named objectHolder of class Computer. You will notice we have passed in two arguments at the time of creation of the object. Now this invokes the constructor of Computer class. Everything that’s inside of the constructor gets run at this instance. Finally, we are assigning the objectHolder to the array’s current index. Summing up we will have five objects assigned to the array at the end of our loop.

5. Line 25 to 30 is another loop. Here we invoke the displayDeviceInfo method of the class Computer on each object stored in the array computers. On invoking the method, we get the information of the device we’ve entered at the time of creation of the object.

Following is the output of our program. You will see Object destroyed being printed several time. This is because we have a destructor method in our computer class.