×
WORK HARD IN SILENCE; LET SUCCESS MAKE THE NOISE
--Your friends at LectureNotes
Close

Note for Object Oriented Programming Using Cpp - OOP By Aman Kumar

  • Object Oriented Programming Using Cpp - OOP
  • Note
  • 5 Views
  • Uploaded 8 months ago
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-2

eating. An object is an instance of a class. Class - A class can be defined as a template/blueprint that describes the behaviors/states that object of its type support. Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed. Instant Variables - Each object has its unique set of instant variables. An object's state is created by the values assigned to these instant variables. C++ Program Structure: Let us look at a simple code that would print the words Hello World. #include <iostream> using namespace std; // main() is where program execution begins. int main() { cout << "Hello World"; // prints Hello World return 0; } Let us look various parts of the above program: The C++ language defines several headers, which contain information that is either necessary or useful to your program. For this program, the header <iostream> is needed.

Text from page-3

The line using namespace std; tells the compiler to use the std namespace. Namespaces are a relatively recent addition to C++. The next line // main() is where program execution begins. is a single-line comment available in C++. Single-line comments begin with // and stop at the end of the line. The line int main() is the main function where program execution begins. The next line cout << "This is my first C++ program."; causes the message "This is my first C++ program" to be displayed on the screen. The next line return 0; terminates main( )function and causes it to return the value 0 to the calling process. The Input / Output operator: C++ is able to input and output the built-in data types using the stream extraction operator >> and the stream insertion operator <<. The stream insertion and stream extraction operators also can be overloaded to perform input and output for user-defined types like an object. Character And String Literals: A character literal is composed of a constant character. It is represented by the character surrounded by single quotation marks. There are two kinds of character literals: Narrow-character literals of type char, for example 'a' Wide-character literals of type wchar_t, for example L'a' A string literal represents a sequence of characters that together

Text from page-4

form a null-terminated string. The characters must be enclosed between double quotation marks. There are the following kinds of string literals: Narrow string literals, represented as "xxx". Wide string literals, represented as L"xxx". Comments C++ supports single-line and multi-line comments. All characters available inside any comment are ignored by C++ compiler. C++ comments start with /* and end with */. For example: /* This is a comment */ /* C++ comments can also * span multiple lines */ A comment can also start with //, extending to the end of the line. For example: #include <iostream> using namespace std; main() { cout << "Hello World"; // prints Hello World return 0; }

Text from page-5

When the above code is compiled, it will ignore // prints Hello World and final executable will produce the following result: Hello World Variable: The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because C++ is case-sensitive: There are following basic types of variable in C++ as explained in last chapter: Type Description bool Stores either value true or false. char Typically a single octet(one byte). This is an integer type. int The most natural size of integer for the machine. float A single-precision floating point value. double A double-precision floating point value. void Represents the absence of type. wchar_t A wide character type. C++ also allows to define various other types of variables, which we will cover in subsequent chapters like Enumeration, Pointer, Array, Reference, Data structures, and Classes. Variable Definition in C++: A variable definition specifies a data type, and contains a list of one or more variables of that type as follows:

Lecture Notes