'Tips/C++ Eng'에 해당하는 글 26건

  • Always have the same name as the class
  • Does not return anything
  • Use : syntax to initialize member variables


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Rectangle.h"
 
/*
Rectangle(): _width{ 1 }, _height{ 1 }
{ }


*/


int main()
{
    int x;                        //garbage value
    int y{};
    int z{};
 
    Rectangle unintialized;   //garbage value
    Rectangle value{};
    Rectangle aggregate{ 2,3 };
 
    value._width = 2;
    value._height = 3;
}
cs


This contents comes from edx : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Constructor[생성자]  (0) 2015.11.28
Introducing Classes  (0) 2015.11.28
Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

Creating Classes and Members

In C++, a class is a programming construct that you can use to define your own custom types. When you create a class, you are effectively creating a blueprint for the type. The class defines the behaviors and characteristics, or class members, which are shared by all instances of the class. You represent these behaviors and characteristics by defining methods and fields within your class.

Suppose you create a class to represent a rectangle shape in your program.  You use the class keyword to declare a class, as shown in the following example:

//Declaring a Class
class Rectangle
{
public:
    int _width;
    int _height;

};

Here we have declared a class called Rectangle and given it two public member variables called _width and _height, that will be used to represent the width and height of our rectangle.   Note that they are accessible directly because they are public, as a result of the public: modifier.

Using a Class

Now that we have a class created to represent a rectangle, we can use that in our code to create instances of a rectangle in our program.  When we create a new rectangle from this class, it is known as a rectangle object and will be given a unique name.  That way ,we can refer to it in our program directly and distinguish it from other rectangle instances that we might create, should our program require more than one.

void main()
{
     Rectangle outer;
     Rectangle inner;    

     outer._width = 10;
     outer._height = 10;

     inner._width = 5;
     inner._height = 5;
}

In this sample code, we have created two rectangle objects called outer and inner.   Then, using what is known as "dot notation" or the dot operator, we provide values for the width and height of each rectangle.  The outer rectangle is 10 x 10 and the inner rectangle is 5x5.

This contents comes from edx : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Constructor[생성자]  (0) 2015.11.28
Introducing Classes  (0) 2015.11.28
Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

MSDN defines storage class as, "A storage class in the context of C++ variable declarations is a type specifier that governs the lifetime, linkage, and memory location of objects."

Lifetime refers to how long the variable "hangs around" in memory from the point at which it is declared and the point at which it is destroyed (the memory it used is released).  For the most part, once a variable goes out of scope, its memory will be released back to the operating system for reuse.

Linkage refers to the visibility of a variable outside of the file that contains it. 

Memory location refers to the place in which the variable is found in memory.  This doesn't refer to the physical memory address as you might expect but more to the logical division of memory that applies to a running application.  There are two logical memory areas known as the stack[스택] and the heap[]. 

 

출처 : http://pacs.tistory.com/5

The stack is a location in memory where intrinsic data is stored as well as memory addresses (pointers).

스택이란 ? 메모리 위치 (메모리 주소에 저장된 고유한 데이터)

It operates in the form of data structure known as a stack.  Like a cafeteria stack of plates, items are pushed on top of the stack and other items are pushed further down.  To remove an item from the stack, it is popped off, used, and discarded.

동작 방식 >> 위에 것이 쌓이면 아래 것이 내려간다.

층을 없애려면 , 한 층을 버려야 한다.

The heap, or free store, is a pool of memory that is used to store objects that dynamically allocated at run time by your application.  An object is what you will learn about in the next topic on object-oriented programming.  You create and destroy objects on the heap by using specific instructions in your program code.

힙 ? Data which is placed by programmer  : Create/ Destroy Object on the heap

동적 할당된 데이터

int *p = new int[10];


Scope [범위] is the term used describe where an identifier is visible in a program. 

프로그램에서 사용하는 식별자.


An identifier is a variable, constant, class, etc.  Your identifier is visible from the point in which you have declared it until the end of its scope.  The following code sample displays different scope for the identifiers used.

1. #include <iostream>
2. int main()
3. {
4.     int total = 0;
5.     for(int i = 1; i <= 10; i++)
6.     {
7.          total += i;
8.     }
9.     std::cout << "The sum of the numbers 1 to 10 is " << total << std::endl;
10.    std::cout << "Current value of i is " << i << std::cout;

11. return 0;
12. }

In the previous code, the variable total is declared inside main() but outside of the for loop.  This means that total is visible (in scope) for the entire main() method, which also includes inside the for loop.  However, the variable i is declared inside the for loop's initialization section and is therefore constrained to the scope of the for loop.   The code at line 10 will result in an error in C++ that indicates the variable is undefined.    Anyplace other than inside the for loop is out of scope for the variable i.

C++ makes use of the following keywords that apply to storage classes:

  • static - identifiers declared with static are allocated when the program starts and deallocated when the program execution ends.  Declaring a variable as static in a function means that the variable will retain its value between calls to the function.
  • extern - used to declare an object that is defined in another translation unit of within the enclosing scope but has an external linkage.
  • thread_local - declares that the identifier is only accessible on the thread in which it is created.  This prevents sharing of the identifier across multiple threads in the same application.   This is part of the C++11 standard.

This contents comes from : edx : Introduction to C++


저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Constructor[생성자]  (0) 2015.11.28
Introducing Classes  (0) 2015.11.28
Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

The for loop executes a block of code repeatedly until the specified expression evaluates to false. You can define a for loop as follows.

for ([initializer(s)]; [condition]; [iterator]) 
{
   // code to repeat goes here
}

The [initializer(s)] portion is used to initialize a value, or values, as a counter for the loop. On each iteration, the loop checks that the value of the counter is within the range to execute the for loop, specified in the [condition] portion., and if so, execute the body of the loop.   At then end of each loop iteration, the[iterator] section is responsible for incrementing the loop counter.

The following code example shows how to use a for loop to execute a code block 10 times.

for Loop
for (int i = 0 ; i < 10; i++) 
{
    // Code to execute.
}
In this example, i = 0; is the initializer, i < 10; is the condition, and i++ is the iterator. 


This contents come from edx : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Introducing Classes  (0) 2015.11.28
Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

The C++ conditional operator is also known as a ternary operator because it takes three operands.  How this operator functions is somewhat similar to an if statement or a switch statement, but in a more compact form and for one single Boolean value with one of two possible outputs.   That is to say, the first operand is evaluated as a Boolean result.  If the result is true, then the second operand will be the one evaluated.  Otherwise, the third operand will be evaluated.   A sample helps amplify this.

#include <iostream> 
using namespace std; 
int main() 

     int i = 1, j = 2; 
     cout << ( i > j ? i : j ) << " is greater." << endl; 
}

In this example, we have two integer variables, i and j which are initialized to 1 and 2 respectively.  The ternary operator is embedded inside the cout statement and essentially follows this pattern:

  1. it checks whether i is greater than j
  2. it outputs the proper numeric value along with is greater.

In the code example here, j is greater than i so the condition evaluates to false and the value for j (2), is output to the console along with the text is greater.  In other words, the output is "2 is greater."  If i was 5 and j was 2, the output would be, "5 is greater." 

i > j ? i : j where i is greater than j then the bold value is selected

i > j ? i : j where j is greater than i, then the bold value is selected

This contents come from : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01
Unions Quiz  (0) 2015.10.27

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

switch Statments

Tips/C++ Eng 2015.11.04 00:13

If there are too many else if statements, code can become messy and difficult to follow. In this scenario, a better solution is to use a switch statement. The switch statement simply replaces multiple else ifstatements. The following sample shows how you can use a switch statement to replace a collection of else if clauses.

switch Statement

char response = 'y';
switch (response)
{
   case 'y':
      // Block of code executes if the value of response is y.
      break;
   case 'Y':
      // Block of code executes if the value of response is Y.
      break;
   case 'n':
      // Block of code executes if the value of response is n.
      break;
   default:
      // Block executes if none of the above conditions are met.
      break;
}

Notice that there is a block labeled default:. This block of code will execute when none of the other blocks match.  The default block is optional.

In each case statement, notice the break keyword. This causes control to jump to the end of the switchafter processing the block of code. If you omit the break keyword, the application may not perform as you anticipate.  In other languages, such as C#, omitting the break; keyword will cause the code to no longer compile. 

Without the break statement, the code will "fall through" to the remaining cases until it encounters a break statement.   Be very careful in using fall through logic in your switch statements.  The most common use for a fall through scenario is when you want to handle multiple cases with a single statement or set of statements.

If you are coming from another programming language, such as C#, that also uses the switch statement, you might notice that in the C# language, you can use string values in your switch statements and don't have to use integers or enumerated types.  C++ switch statements support the following data types as expressions:

    • intrinsic data types such as int or char
    • enumerations

This contents come from Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01
Unions Quiz  (0) 2015.10.27
Enumerations  (0) 2015.10.27

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

Because you will start to learn about control statements in C++, it's important to understand the C++ operators that exist in the language first, as they play an important role in control statements.

You will work with comparison operators to determine if values are equal, greater, or less than each other.  C++ also allows you to use mathematical operators for incrementing values to help control the number of iterations in a loop.  You can also make use of bitwise operators to speed up some operations in your code. 

OperatorDescription
+addition
-subtraction
*multiplication
/division
%modulo
+= (y += x)same as y = y + x
-= (y -= x)same as y = y - x
*= (y *= x)same as y = y * x
++increment by 1
--decrement by 1
==equal to
!=not equal to
>greater than
<less than
>=greater than or equal to
<=less than or equal to
&&logical AND
||logical OR
!logical NOT

This contents come from edx : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01
Unions Quiz  (0) 2015.10.27
Enumerations  (0) 2015.10.27
Unions  (0) 2015.10.27

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret

Unions Quiz

Tips/C++ Eng 2015.10.27 16:27

UNIONS

(1/1 point)

Consider the following code snippet:

1. union numericUnion

2. {

3. int intValue;

4. long longValue;

5. double doubleValue;

6. };

7. numericUnion myUnion;

8. myUnion.intValue = 3;

9. cout << myUnion.intValue << endl;

10. myUnion.doubleValue = 4.5;

11. cout << myUnion.doubleValue << endl;

12. cout << myUnion.intValue; cout << endl;

What value will be output to the console window on line 12?

더보기


This contents come from edx : introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

switch Statments  (0) 2015.11.04
Introducing Operators  (0) 2015.11.01
Unions Quiz  (0) 2015.10.27
Enumerations  (0) 2015.10.27
Unions  (0) 2015.10.27
Structure [구조체]  (0) 2015.10.21

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret