Category Archives: C++

String Refreshing

//Program to assign string and display the same

//specifiying the array string size

Assigning string character by character

//Read a string and display it

//It will not allow string

so we need gets(str)
code:

with gets we can print line with space: Zaki Live

//simple printf

//using puts

//putchar

Program to find length of a given string:

Using string related library function:

//string copy with out library function

//using strcpy library function

Concatenated String:

Using library function:

 

 

C refresh for logic development

Code:

Output:

Details about Pointer

 

 

 

C Refresh

Palindrome C code

http://dharanyadevi.blogspot.com/2012/04/palindrome-in-c-without-using-string.html

String and Hackerearth

String Upper:

String Lower:

Lower Upper Both:

Hackerearth Problem:: Toggle String

Another Approach:

Another Approach:

 

Helpful Link: http://www.programmingsimplified.com/c/program/c-program-change-case

Vector in C++

Vector Codes:

Vector Copy:

String:

Stack:

Queue :

Priority Queue:

Iterator:

Set:

Map:

String Stream:

Good websites for learning by example:
http://www.sanfoundry.com/cpp-program-implement-vector-stl/

http://www.progkriya.org/gyan/stl.html

A good Array Example

Hackerrank problem:

 

A good example of pointer code at Hacker Rank

A good example of pointer code at Hacker Rank:

 

String Compare in C/C++

http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c

Flags in C

Reference:
https://www.daniweb.com/programming/software-development/threads/201233/use-of-flag-in-c

How to count numbers from string ?

code:

 

C++ Overloading (Operator and Function)

Function Overloading in C++

Operator Overloading in c++

we would have to pass two arguments for each operand as follows:

Box operator+(const Box&, const Box&);

example:

Output:

2016-01-07 01_08_59-C__Users_ZakiHP_Documents_GitHub_c++_operator-overload.exe

Reference:
http://www.tutorialspoint.com/cplusplus/cpp_overloading.htm

There are some examples for various operators and list.You can go to reference link for this.Here I am pausing this as I am going to switvh to java as java is good for OOP. But I will be back to this C++ series if I get my free time again. 🙂

And If I get time I have a plan to start blogging about C++ vector, STL and Template library that is very helpful for contest programming.

C++: Inheritance

One of the most important conecpts in object oriented programming is that of inheritance.Inheritance allows us to define a class int terms of another class,which makes it easier to create  and maintain an applicatrion.This also provides an opportunity to resue code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class.This existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the is a relationship.For example, mammal Is-A animal,monkey Is-A mammal hence monkey Is-A animal .And as well as and so on.

Base & Derived classes:

A class can be derived from more than one classes, which means it can inherit data and functions from multiple base classes. T o define a derived class we use a class derivation list to specify the base class(es). A class derivation list names one or more base classes and has the form:

 

where access specifier is one public,protected or private, and base-class is the name of a previously defined class.If the access-specifier is not used, then it is private by default.

consider a base class Shape and it s derived class Rectangle as follows:

Output:

2016-01-04 23_46_23-inheritance-derivedclass.cpp - Code__Blocks 13.12

Access Control and Inheritance:

A derived class can access all the non-private members of its base class.Thus base-class members that should not be accessible to the member functions of derived classes should be declared private in the base class.

We can summarize the different access types according to who can accesss them in the following way:

Access                    Public    Protected  Private
Same Class              yes          yes             yes
Derived classes      yes         yes             no
Outside classes      yes         no             no

A derived class inherits all base class method with the following exceptions:
– Constructors, destructors and copy constructors of the base class.
– Overloaded operators of the  base class.
– The friend function of the base class.

Type of Inheritance:

When deriving a class from a base class the base class may be inherited through public,protected or private inheritance.The type of inheritance is specified by the access-specifier as explained above.

We hardly use protected or private inheritance but public inheritance is commonly used.While using different type of inheritance following rules are applied:

Public Inheritance: When deriving a class freom a public base class,public members of the base class become public members of the derived class and protected members of the base class become the protected members of the derived class .A base class private members are never accessible directly from a derived class, but can be acccessible through calls to the public and protected members of the base class.

Protected Inheritanc:When deriving from a protected base class,public and protected members of the base class become protectecd members of the derived class.

Private Inheritance:When deriving from a private base class,public and protected members of the base class become private members of the derived class.

Muktiple Inheritances:

A C++ class can inherit members from more than one class and here is the exrended syntax:

class derived-class:access basA,access baseB,access baseC……

Here the example goes:

Output:
2016-01-05 04_10_00-C__Users_ZakiHP_Documents_GitHub_c++_inheritance-derivedclass-multiple.exe

C++ OOP: Constructor Destructor

The class constructor:

A class constructor is a special member function of a class that is executed whenever we create neww objects of that class

A costructor will have exact same name as the class and it does not have any return type at all, not even void.Constructors can be very useful for setting initial values for certain member variables.

Following example wxpalins the concept of constructor:

Parameterized Constructor:

A default constructor does not have any parameter, but if you need, a constructor can have parameters.This helps yout to assign initial value to an object at the time of its creation as shown in the example:

2015-09-30 14_02_06-_D__desktop_desktop september_oop_param.exe_

Using Initialization Lists to Initialize Fields:

In case of parameterized constructor, you can use following syntax to initialize the fields:

Above syntax is equal to the following syntax:

If for a class C, you have multiple fields X,Y,Z etc. to be initialized, then can be use same syntax and separate the fields by comma as follows:

 

The class Destructor:

A destructor is a special member fucntion that is executed whenever an object of it’s class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class.

A destructor will have exact same name as the class prefixed with a tilde(~) and it can neither return a value nor can it take any paramaters.Destructor can be very useful for releasing resources before coming out of the program liek

2015-09-27 00_53_49-_D__desktop_desktop september_oop_destructor.exe_