Skip to main content

Posts

Recent posts

C-style strings vs String Class.

Here, we are going to learn about, what are C-Style Strings and what is String class . So, let's start by giving you the introduction. C-Style String is nothing but, an array of characters, and from the term array we can surely assume that these are static in size i.e. the size of these C-Style strings cannot be increased or decreased. String Class is a Built-in class that provide us much more functionality than C-Style String. C-STYLE STRINGS Before learning the String class, which is full-fledged feature of C++, we should rather start by taking a look at some important things about C-Style String. C-Style Strings are actually array of characters that are terminated by a null character "\0". If you are confused with, why null character? The reason is that, It helps us to define, upto which index we have some useful data present in our character array, and after null character there may or may not be some garbage values of characters. Let's first start by showing a
Recent posts

Pointers.

I have already introduced pointers in the C++ Derived Data Types post , But, here I will be showing some practical examples and at the end of this slide, you will be understanding pointers like they are, obvious to you. So, let' start by reminding you the basic syntax :- <data type> *<name_of_pointer> ; Below is the simple program for pointer declaration and initialization. #include<iostream> using namespace std; int main (){ int a = 20 ; int *b; // Declaration of pointer b b = &a; // Initialization of pointer b (& is used to extract the address of a) int *c = b; // Initialization of pointer c to pointer b cout << a << endl << b << endl << c << endl; } Okay, after reminding the basic syntax of pointers, you have to memorize the reason why we generally use pointers, I have already discussed that also, but, let me briefly describe it. One of the memory section in our Comp

Practice Problems #1.

Below are 6 problems related to arrays, decision making statements etc. No solution will be provided for these problems except Bonus Problem. Problem 1 - Your program should print  "The weather is Good" if today's temperature is greater than 20 degree and less than 25 degree,  "The weather is little cold" if today's temperature is less than 20 degree and greater than 15 degree,  "The weather is cold" if today's temperature is less than 15 degree. "The weather is little warm" if today's temperature is greater than 25 degree but less than 30 degree. "The weather is warm" if today's temperature is greater than greater than 30 degree and less than 40 degree. "The weather is really hot" if today's temperature is greater than 40 degree. Try, to not to confuse yourself with all these statements and print accordingly to the condition matching the user's input. Problem 2 - Try to create a program with foll

The Loop Statements.

In this slide we try to figure out what are loops and why they are important and using them with a couple of examples. After that we will try to understand some other statements like Break , Continue and Goto statement. Loops are used to perform some task repeatedly until some condition remains true. for e.g. take this blog, we will keep learning C++ until we don't get to a level, where we can write basic C++ program very easily. Let's discuss these, one by one - for loop Statement. while loop Statement. do-while loop Statement. Let's Start our discussion. for loop for loop is one of the most widely used loop, and I myself most of time use for loop. for loop is used when you very  well know that how much repetitions you need to perform. For example, counting numbers upto 100, and the time we reach hundred we stop. Syntax is shown in the below snippet -  #include<iostream> using namespace std; int main () { for ( initialization ; condition ; increment/decrement

Decision Making Statements in C++.

The smallest Independent Unit in a C++ program is a statement i.e. You can relate it to sentences of a natural language. Because, Just like sentences are terminated with a period (.), similarly the statements are also terminated with semicolon (;) The simplest form of a program statement is the empty statement i.e.( ;) (NOTE - I will not be explaining this topic so much because, It is mostly known by every programmer, doesn't not matter if he/she is a beginner or a professional) Let's discuss the Decision Making Statements one by one. 1. If Statement - These statements are used when you want something to happen if your condition is true. Consider an example like this - If it's rainy outside I will bring an Umbrella with me. #include<iostream> using namespace std; int main () { bool rain = true ; if (rain == true ) // It can simply be written as if(rain){} { cout << "Take an Umbrella with you" << endl; } } A

Expressions and Operators in C++.

An Expression is composed one more or more operand and operations to be applied on them.  In C++ Operand can by anything like an integer, a float, or a array and the operations applied to the operands are represented by Operators .  We can classify the Operators in three Categories - Unary Operators. Binary Operators. Ternary Operator. ( will be discussed at last ) (Don't try to relate anything about Binary Operators and Binary Numbers.) 1. Unary Operators - Unary operators are those which act on only one operand.(Do you know any operators like this? YES, you have seen them previously), The address of (ampersand (&)) operators and de-reference (*) operator. These two operators are Unary operators, We have used them like -  #include<iostream> using namespace std; int main () { int a = 10 ; // Initialization of variable 'a' int *b = &a; // Storing address using unary operators '*' and '&' in b return 0 ; } You c

C++ User defined Data Types.

I had explained Built-in and Derived Data types in previous blogs. Now, Let's resume our topic from User Defined Data types. Simple definition - User defined data types are the data types which the User define by himself/herself. We need these data types because there are certain cases when the pre-defined data types won't help us, e.g. think of a list, which contain every information about every particular student learning in a school. Like Student name, Student IDs, Student class, Student section etc. You can think of using arrays, but arrays can only contain one field of data of built-in datatypes like int, char etc. Which is not sufficient. Now, We will discuss the following topics -  Class. Structures. Enumeration. Union. So, let's start our discussion. Classes Class is a data type present in C++, but not in C. Classes can be considered as a mechanism that allows us to create new types(of data), and we can declare and use objects of that type. A class definition cons