|
|
|
|
![]() ![]() |
Jan 1 2008, 01:47 PM
Post
#1
|
|
|
Newbie [Level 2] ![]() ![]() Group: [HOSTED] Posts: 37 Joined: 31-December 07 Member No.: 55,510 |
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 - CODE #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. This post has been edited by jlhaslip: Jan 1 2008, 08:29 PM |
|
|
|
Jan 1 2008, 06:46 PM
Post
#2
|
|
|
Newbie [Level 2] ![]() ![]() Group: [HOSTED] Posts: 37 Joined: 31-December 07 Member No.: 55,510 |
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. CODE #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 - CODE #include<iostream.h> #include<conio.h> void main() { float a=12.5,b=13.5,c; c=a+b; cout<<"c = "<<c; getch(); } |
|
|
|
Jan 1 2008, 08:34 PM
Post
#3
|
|
|
A computer once beat me at chess, but it was no match for me at kick boxing. ![]() Group: [MODERATOR] Posts: 3,882 Joined: 24-July 05 From: In Trouble Again... still? Member No.: 9,787 ![]() |
Cleaned up some Postings which were off topic.
Let's stay on Topic here... |
|
|
|
Jan 2 2008, 10:30 AM
Post
#4
|
|
|
Newbie [Level 2] ![]() ![]() Group: [HOSTED] Posts: 37 Joined: 31-December 07 Member No.: 55,510 |
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 - CODE #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 - CODE #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 - CODE #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 - CODE #include <iostream>
#include<conio.h> #include <string> using namespace std; int main () { string mystring = "This is a string"; cout << mystring; return 0; getch(); } This post has been edited by cncinfotech: Jan 2 2008, 10:54 AM |
|
|
|
Jan 6 2008, 05:33 PM
Post
#5
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: [HOSTED] Posts: 239 Joined: 10-December 07 From: India Member No.: 54,553 |
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 language
I 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. |
|
|
|
Jan 26 2008, 02:15 PM
Post
#6
|
|
|
Newbie [Level 2] ![]() ![]() Group: [HOSTED] Posts: 37 Joined: 31-December 07 Member No.: 55,510 |
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 - CODE 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 - CODE 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 - CODE 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. |
|
|
|
Jan 27 2008, 10:13 AM
Post
#7
|
|
|
Newbie [Level 2] ![]() ![]() Group: [HOSTED] Posts: 37 Joined: 31-December 07 Member No.: 55,510 |
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. CODE int age=19; For characters value being assigned should be within single quotes. Char type variable can hold only one character. CODE char choice=’y’; |
|
|
|
Mar 12 2008, 07:41 PM
Post
#8
|
|
|
Newbie [Level 2] ![]() ![]() Group: [HOSTED] Posts: 37 Joined: 31-December 07 Member No.: 55,510 |
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). ( © 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. ![]() 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. This post has been edited by cncinfotech: Mar 12 2008, 07:43 PM |