Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Tech-prog(theory).docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
113.55 Кб
Скачать

Identifier or function name

The identifier (name) of function is set just as also any other identifier. In this example we created function with the simple_function identifier (simple - idle time).

Declaration

int sum (int a, int b)

{int c;

c = a + b;

return c;}

int main()

{int s;

s = sum(2,2); // вызов функции

cout << s;

return 0;}

В результате выполнения программы, на экран будет выведено: 4.

6) Declaration of classes in the programming language C++.

Declaration and usage

C++ classes have their own members. These members include variables (including other structures and classes), functions (specific identifiers or overloaded operators) known as methods, constructors and destructors. Members are declared to be either publicly or privately accessible using the public: and private: access specifiers respectively. Any member encountered after a specifier will have the associated access until another specifier is encountered. There is also inheritance between classes which can make use of the protected: specifier.

Basic declaration and member variables

Classes are declared with the class or struct keyword. Declaration of members are placed within this declaration.

class person

{public: string name; int age;};

The above definitions are functionally equivalent. Either code will define objects of type person as having two public data members, name and age. The semicolons after the closing braces are mandatory.

After one of these declarations (but not both), person can be used as follows to create newly defined variables of the person datatype:

#include <iostream>

#include <string>

using namespace std;

class person

{public:

string name;

int age;};

int main()

{ person a, b;

a.name = "Calvin";

b.name = "Hobbes";

a.age = 30;

b.age = 20;

cout << a.name << ": " << a.age << endl;

cout << b.name << ": " << b.age << endl;

return 0;}

Executing the above code will output

Calvin: 30

Hobbes: 20

7) String functions in the programming language Java.

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.

The Java platform provides the String class to create and manipulate strings.

Creating Strings:

The most direct way to create a string is to write: String greeting = "Hello world!";

Whenever it encounters a string literal in your code, the compiler creates a String object with its valuein this case, "Hello world!'.

As with any other object, you can create String objects by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:

public class StringDemo{

public static void main(String args[]){

char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};

String helloString = new String(helloArray);

System.out.println( helloString );}}

This would produce following result: hello

Note: The String class is immutable, so that once it is created a String object cannot be changed. If there is a necessity to make alot of modifications to Strings of characters then you should use String Buffer & String Builder Classes.

String Length:

Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is the length() method, which returns the number of characters contained in the string object.

After the following two lines of code have been executed, len equals 17:

public class StringDemo{

public static void main(String args[]){

String palindrome = "Dot saw I was Tod";

int len = palindrome.length();

System.out.println( "String Length is : " + len );}}

This would produce following result: String Length is : 17

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]