NWEN241 - C++

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/39

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 7:45 AM on 5/16/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

40 Terms

1
New cards

How does C++ extend C?

  • strong type checking

  • new data types

  • Classes for implementing user defined data types

  • object-oriented programming (also supports - procedural and functional).

  • a standard library (STL) for frequently used data types (string, list, stack, queue, vector, hash,…)

  • generic programming, i.e., parameterization of variable types via template

2
New cards

Is C++ a superset of C?

Not anymore.

3
New cards

How do Headers in C++ differ from in C?

  • In standard C++, library headers are not supposed to have an extension.

  • C++ standard neither requires nor forbids an extension for other headers.

  • If a user wants to define with an extension the common choices are- .h / .hh /* .hpp

4
New cards

List the data types their keywords and the modifiers if applicable in C++.

knowt flashcard image
5
New cards

In C++ what are the » and « operators called and what do they do?

With input streams, the extraction operator >> is used to remove values from the stream.

With output streams, the insertion operator << is used to put values in the stream

6
New cards

Give syntax examples of standard input and output.

std::cin>>i;

std::cout<<"Hello World";

7
New cards

State the four predefined standard stream objects, what they do and what I/O hardware they typically apply to.

Unbuffered output is typically handled immediately, where as buffered output is typically stored.

<p>Unbuffered output is typically handled immediately, where as buffered output is typically stored.</p>
8
New cards

What are namespaces? Give a syntax example.

  • Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

  • Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope.

namespace namespace_name
{
	members 
}

9
New cards

What can the members of a namespace be?

Members can be:

  • constants,

  • variables,

  • functions,

  • classes,

  • or another namespace(nested namespaces)

10
New cards

What is the visibility of members in a namespace? How can members of a namespace be accessed? Give a syntax example of each.

Members are not visible outside its namespace. Two ways to access a namespace member outside its namespace:

  • Use namespace_name::identifier syntax

  • Use the using keyword to access specific or all members of a namespace

// namespaces
#include <iostream>
using namespace std;

namespace foo
{
	int value() { return 5; }
}

namespace bar
{
	const double pi = 3.1416;
	double value() { return 2*pi; }
}

int main () {
	// No need for std::cout
	cout << foo::value() << '\n';
	cout << bar::value() << '\n';
	cout << bar::pi << '\n';
	return 0;
}

11
New cards

What does a C++ program consist of?

  • 1 or more header files

  • 1 or more C++ source files

12
New cards

Give the general syntax for defining a class in C++

class class_identifier {
	class_member_list
};

13
New cards

What are the commands for compiling and running a C++ program?

Compile: g++ hello.cpp –o helloex

Run: ./helloex

14
New cards

What are the members access specifiers of a class?

  • Private - can only be accessed by member functions (and friends) and not accessible by descendant classes

  • Public - an be accessed outside the class and inherited by descendant classes

  • Protected - can only be accessed by member functions (and friends) and inherited by descendant classes

No access specifier the default is private

15
New cards

What are the types of constructors?

Default Constructors (Non – parameterized Constructor)

  • Accepts no arguments

  • class_name()

Parameterized constructor

  • Accepts arguments

  • class_name(parameters)

Copy constructor

  • Copies another existing object

  • class_name (const class_name & )

& - Reference operator, used to provide an alternative name for an existing variable

16
New cards

Provide syntax examples of the different constructors.

class StudentInfo {
	int student_id;
	string name;

  public:
	void print();

	// Default Constructor
	StudentInfo()
	{
		student_id = 0;
		name="Sam"; }

	StudentInfo(int, string);
  };

// Parameterized Constructor
StudentInfo::StudentInfo(int i, string s){
	student_id = i;
	name = s;
}

17
New cards

How can a member function be restricted from modifying member variables?

BY using the const keyword at the end of declaration: void print() const;

18
New cards

What are the two ways that member functions can be declared?

  • By specifying the function prototype - must still be implemented separately somewhere else

  • By specifying the function implementation

19
New cards

What is an inline function and how is it made?

When a function is inline, the compiler does not make a function call. The code of the function is used in place of the function call (function call is replaced by function code and appropriate argument substitutions made). Compiled code may be slightly larger, but will execute faster because function call overhead is avoided.

Including the implementation of a function within the class definition is an implicit request (to the compiler) to make a function inline.

To explicitly request to make member functions inline add the inline keyword before return type in function declaration and definition.

20
New cards

Why might inline function requests be denied by the compiler?

  • Function is recursive

  • Function contains switch or goto statement

  • Function return type is other than void, and the return statement doesn’t exist in function body

  • Function contains a loop (for, while, do-while)

  • Function contains static variables

21
New cards

How can members of a class be accessed?

Member access operator, .

class Time {
  public:
	void set(int, int, int);
	void print() const;
	Time();
	Time(int, int, int);
  private:
	int hour;
	int minute;
	int second;
};
myTime.set(10, 30, 0);
myTime.hour = 12; // Fails

22
New cards

How is the static keyword used in a class?

  • A static member variable is a variable that is shared by all instances of a class.

  • Static member variables are often used to declare class constants.

  • A static member function is a special member function, which is used to access only static data members

23
New cards

Define function overloading in C++

Create two or more member functions having the same name but different parameters declared in the same scope.

24
New cards

What is operator overloading in C++?

Operators have different implementations (meanings) with different arguments.

The extraction operator >> and the stream insertion operator << are overloaded. They perform the I / O operation based on the type of argument.

25
New cards

What is good practice in where to declare and implement Classes and Member Functions?

  • Good programming practice is to declare the class in a header file. Extensions: .h, .hh, .hpp

  • Separate the implementation of the member functions (and possibly constructors) in another source file. Extensions: .cpp, .cc, .cp

26
New cards

What is a key difference between Java and C++ regarding static variables?

C++ allows static local variables inside functions/methods, but Java does not.

void func() {
	static int x = 0;
	x++;
	printf("%d\n", x);
} // Calling func() repeatedly prints: 1 2 3

27
New cards

What is the rule regarding declaring and defining static members in a class?

Static members are only declared in a class declaration. They must be explicitly defined outside the class using the scope resolution operator. The static keyword is only used with the declaration of a static member, inside the class definition, but not with the definition of that static member.

class Time {
  public:
	void set(int, int, int);
	void print() const;
	static int getCounter();
	Time();
	Time(int, int, int);

  private:
	int hour;
	int minute;
	int second;
	static int counter;
};

// Initialize static member variable
int Time::counter = 0;

// Define static member function
int Time::getCounter()
{
	return counter;
}

28
New cards
29
New cards
30
New cards
31
New cards
32
New cards
33
New cards
34
New cards
35
New cards
36
New cards
37
New cards
38
New cards
39
New cards
40
New cards