Jump to content
xisto Community
cncinfotech

Let Me Teach You C++ I would like teach u c++ if you are beginner.

Recommended Posts

As you know c++ is enhance version of c, we can say it is superset of c. The most important facilities that c++ adds on c++ on to c are classes, inheritance, function overloading and operator overloading. These features enable creating of abstract data types, inherit properties from existing data types and support polymorphism, thereby making c++ a truly object oriented language.

Have a look of a basic c++ program -

#include<iostream.h>int main(){cout<<"C++ is better than c.";return 0;}
output- C++ is better than c.

The header file iostream should be included at the beginning of all programs that use input/output statement. The above function has only one function main(). Execution of program begins from main(). Cout causes the string in quotation marks to be displayed on the screen. Because we using main with int so we add return 0; which is int type return in the end of program.

Notice from rvalkass:

Please avoid double posting. If you have something to add, edit your post. Also, try to make proper use of BBCodes. Code tags have been added around the code.

Notice from jlhaslip:
Removed unneccesary reference from Posting

Edited by jlhaslip (see edit history)

Share this post


Link to post
Share on other sites

Let's continue...............
There are three datatypes in c++.
(1) User- defined ( Structure, union, class, enumeration)
(2) Built-in type (int, char, float, double)
(3) Derived types (array, function, pointers)

First we will discuss about int and float which is used for storing numeric data.
Here is program for addition two numbers which use int data type.

#include<iostream.h>#include<conio.h>void main(){int a=12,b=13,c;		 c=a+b;cout<<"c = "<<c;getch();}

In above program if you set a=12.5 and b=13.5, you will found answer will be 25.
That's why we use float datatype here and program will be -
#include<iostream.h>#include<conio.h>void main(){float a=12.5,b=13.5,c;		 c=a+b;cout<<"c = "<<c;getch();}
Notice from jlhaslip:
Removed unnecessary reference from Posting.
Merged Postings.
If the Member continues requiring to be moderated for each Posting they make, I will dis-allow posting until they confirm that they have read the Xisto Readme file and can obey the common sense rules of the Board.

Share this post


Link to post
Share on other sites

char: This keyword is used to declare characters. The size of each character is 8 bits. i.e., 1 byte. The characters that can be used with this data type are ASCII characters. A character is enclosed within single quotes.

 

 

Example -

#include<iostream.h>#include<conio.h>main(){char c1='f';cout<<"size of char in bytes="<<sizeof(char);cout<<"C1 represents "<<c1;getch();}

An array of characters can be used to contain a C-style string in C++. For example:

char example[] = "Hello world";

 

long:- This long keyword is used for declaring longer numbers. i.e, numbers of length 32 bits.

 

Example -

#include<iostream.h>#include<conio.h>void main(){long l1;clrscr();cout<<"size of long ="<<sizeof(l1);getch();}

Double - It is used to declare floating point decimal numbers. Typically double will hold a greater range than float and long.

 

Example -

#include <iostream>#include<conio.h>using namespace std;int main(){   double x = 12.0;   cout.precision(2);			  // Precision 2   cout << " By default:   " << x << endl;   cout << " showpoint:  " << showpoint  << x << endl;   cout << " fixed:	  " << fixed	  << x << endl;   cout << " scientific: " << scientific << x << endl;   return 0;   getch();}

String - Variables that can store non-numerical values that are longer than one single character are known as strings. The C++ language library provides support for strings through the standard string class. We need to include an additional header file in our source code: <string> and have access to the std namespace.

 

example -

#include <iostream>#include<conio.h>#include <string>using namespace std;int main (){  string mystring = "This is a string";  cout << mystring;  return 0;  getch();}

Edited by cncinfotech (see edit history)

Share this post


Link to post
Share on other sites

Hi I am really looking forward to learn c++,I have actually learnt the basics but it still isn't clear to me ,It would be good if you keep your this C++ thread alive and please teach me anothers more about this great programming languageI haven't seen the above post made by you,because I haven't got much time,I need to go now.but hope you keep this good work going and I would see the above post tommorrow,surely Thanks.

Share this post


Link to post
Share on other sites

Derived types (array, function, pointers) - Please continue to visit on this thread. I will explain them later with example.

 

How to declare variable -

If you are writing a C++ program,Variables must be declared before they are used in executable statement. In C++, variable can be declare anywhere in scope. It makes c++ much easier in use and this feature reduce errors.

 

Dynamic initilization of variable -

As we know dynamic initilization is extremly used in Object Oriented Language, You can also use

dynamic initilization in c++. Here is an example -

main(){ int age= 19;}
Here we initilize value of variable at runtime. Both the declaration and initilization of a variable can be done simultaneously.

Note - There are some rules to declare varibles in program. You will see them in my upcoming posts.

 

Output operator in C++

cout is used as output oprator in C++. The statement of cout is given below -

main(){cout<<"C++ is highlevel language";}
cout cause the string in quatation mark to be displayed on the screen.There are two new feature introduce by c++. First one is cout and second one is <<. cout represents the standard output stream. The standard output stream used to display result of program or any statement on the screen. The standard output stream can also redirect the output to other output device.

Well if u want to put any variable with output opearator (cout), the statement may be look like this -

main(){cout<<"Iam "<<age<<"Year old";}
Well opearator << is called insertion operator.It sends the contents (or value) of variable on its right to the object on its left.

Share this post


Link to post
Share on other sites

Variable declaration rules- There are some rules regarding naming of variables as follows –

(1) Variable names should not be same as keyword. Kewords are reserved names and they have some special meaning for computer.

(2) Variable name should be start with alphabets A…Z or a…..z or underscore(_). Other letter can be numeric or alphanumeric.

(3) Keep variable length small (should be less than 32 character).

(4) If two variables have same name in same scope, it will create error.

(5) Variable name is case sensitive. It means age and Age have different meaning for compiler.

(6) Blank spaces, periods, slash, comma etc. special characters are not permitted to be used in naming variable.

(7) Variable meaning must suggest the meaning for data.

 

Assigning value or content to variable – Well by using = you can assign numerical value to any variable.

 

int age=19;

For characters value being assigned should be within single quotes. Char type variable can hold only one character.

char choice=’y’;

Share this post


Link to post
Share on other sites

Programming techniques- Computer doesn’t understand your language. It understand only programming languages. So to solve your problem you must program it. It’s on you what language you are choosing. But question arise how to go through programming. For this we can use two techniques-

(1) Flowchart (2) Algorithm

Flowchart – Flowchart is pictorial representation of problem’s solution. In flowchart we use predefined symbols to describe logic of computer program.

A flowchart uses some symbols like oval, parallelogram, rectangle, arrow and connectors.

(a) Oval – The oval is used for only two purpose : to define beginning point of a flowchart (Start) and to show termination point (END).

(<_<Parallelogram – The parallelogram is used for input and output operations.

? Rectangle – The rectangle is used in connection with assignment statement. Assignment statements assign value to a variable. For example int P=1000.

(d) Arrow – Arrow shows flow of information.

(e) Diamond – The diamond is used for decision making in a program.

(f) Connectors – The connector symbol is used to eliminate lines between one part of the flowchart to another.

 

Now if we want to draw a flowchart for SI, we follow following steps-

(1) Draw oval and write START init.

(2) Draw a parallelogram (connect it with oval with the help of arrow) and write “Input P, R, T” init.

(3) Draw a rectangle (connect it with parallelogram with the help of arrow) and write “Calculate SI=P*R*T/100” init.

(4) Again draw a parallelogram (connect it with rectangle with the help of arrow) and write “Print SI” init.

(5) Draw oval (connect it with parallelogram where Print SI ) and write “Stop” init.

 

Note : - Picture is attached.

Posted Image

 

Algorithm- Algorithm is a sequence of precise and unambiguous instructions for solving any problem in a finite no. of operations.

Well we can say, Algorithm is a stepwise approach. Which is precise and to the point. For example you want to make a c program for calculating simple interest while Principle, Rate of interest and Time are already given, then what are the steps involved in it let’s see-

Step – 1 : Input principle, rate and time as P, R, T ( Where P denotes to principle, R denotes to rate of interest and T denotes to Time).

Step – 2 : Use mathematics formula

SI = (p*r*t)/100 to calculate interest. (SI is simple interest).

Step – 3 : Print SI as simple interest.

Step – 4 : Stop.

 

Another good example is related to student divisions is given below-

Step -1 : Input student total and obtained marks as t and o.

Step – 2: Calculate percentage of student with mathematics formula –

P= (o/t)*100 Here p denotes to percentage.

Step – 3 : If P>=60 then print “I division.”

If P>44 and P<60 then print “II division.”

If P>32 and p<45 and then print “III division.”

Else print “Student is failed.”

Step – 4 : Stop.

 

Using Turbo c/c++

 

I run my all c programs in turbo c compiler. For run turbo c compiler, you must go in tc directory of your system by following steps –

CD TC

C:\> TC> CD BIN

C:\TC\BIN>TC

Pressing enter key you will get editing screen of turbo c.

 

Here are steps for writing and running a program.

(1) Goto File menu select New there. A blank file will be shown where ypu can write your program.

(2) In second step you save your program. For this you will select save option in File menu and save your file as filename.cpp.

(3) Now we ready to compile our program. By compilation, we can check syntax error and warning messages. For compile our program we select compile from Compile menu or press Alt + F9.

(4) Next screen shows success message (If there is no error in your program).

(5) Now for executing your program select Run from Run menu or press Ctrl + F9. Now you can see output of your program.

Edited by cncinfotech (see edit history)

Share this post


Link to post
Share on other sites

Keywords (Reserved words) –

Keywords are certain reserved words in “C/C++” library and have predefined meaning. These keywords cannot be used as identifiers. We should be remember –

(1) All keywords should be in lower case. C and C++ are case sensitive languages.

(2) Keywords cannot be used for variable name or function name.

Here are list of some keywords

auto break case char const continue default do double else enum extern float for goto if int long register return short signed sizeto static class switch typedef union unsigned void volatile while

 

Operators

An operator is used for mathematical or logical manipulation in a program. Another definition is -

Operator are specials characters symbols which perform some operations on variables in calculations involved in expressions on program data. We solve many mathematics problem by multiplying, dividing, adding and subtracting different numbers. In C++, ‘+’ is used for adding two numbers, ’-’ for subtracting two numbers, ‘*’ for multiply and ‘/’ for divide.

Operators are divided in following categories – <_<

(1) Arithmetic Operators

(2) Assignment operators

(3) Increment and Decrement operators

(4) Compound assignment operators

(5) Relation and logical Operators

(6) Bitwise and logical Operators

(7) Comma Operator

Share this post


Link to post
Share on other sites

Arithmetic operator- In C++ addition, subtraction, multiplication and other mathematic operation are performed by these operators.

 

If you want to add two or more numbers you can use ‘+’operator this purpose.

a= b+c;

Here first c compiler add b, c and then it will store result in a.

 

If we want to subtract two numbers then we can use ’-’ operator.

a=b-c;

‘/’ operator is used for division purpose.

a=b/c;

 

‘%’ operator is used to store remainder when first operand is divided by second operand.

a=10%7;

Here ‘%’ operator store remainder 3 in a after divide 10 by 7.

 

If you want to multiply two numbers then you have to use ‘*’ operator.

a=b*c;

 

‘++’ operator can be perform two operations – <_<

(1) Pre increment

(2) Post increment

Pre increment – It means first do the operation then use value in expression

Example – int a=20;

b=++a;

When you print a and b value on the screen, both will show 20.

 

Post increment – It means first use the current value in expression then do the operation.

Example – int a=20, b;

b = a++;

See second line, it make b=20 and a=11 because post increment is used.

 

‘--‘operator perform post decrement and pre decrement operations.

 

Example-

 

#include<iostream.h>#include<conio.h>main(){int a = 20int b = a++;cout<<”\n a = ”<<a<<”\n b = ”<<b;b = ++a;cout<<”\n a = ”<<a<<”\n b = ”<<b;a=100;b = a--;cout<<”\n a = ”<<a<<”\n b = ”<<b;b = --a;cout<<”\n a = ”<<a<<”\n b = ”<<b;cout<<”\n a++  = ”<<a++;cout<<”\n b--  =”<<b--;getch();}
Edited by cncinfotech (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.